Skip to content
Advertisement

Tag: arrays

in_array for a combo value (‘test’,’value’)

I’m trying to use in_array or something like it for associative or more complex arrays. This is the normal in_array What I’m trying to search is a pair, like the combination ‘test’ and ‘value’. I can set up the combo to be searched to array(‘test’,’value’) or ‘test’=>’value’ as needed. But how can I do this search if the array to

Replace string in an array with PHP

How can I replace a sub string with some other string for all items of an array in PHP? I don’t want to use a loop to do it. Is there a predefined function in PHP that does exactly that? How can I do that on keys of array? Answer But array_map is just a hidden loop. Why not use

Working with an array with periods in key values

I’m getting data from an array. For some reason the array has key values like [3.3] which I’m having trouble retrieving data from. I have this array [3.3] => First Name [3.6] => Last Name[2] => email@example.com. When I try to call $array[3.3] it returns null, but when I call $array[2] I am given the e-mail. Any ideas? Answer Use

Convert this array to an HTML List

I have this array: OR Every key is the id and value is the id for the childs of this parent. ID of 0 means that (3 & 6) have no parent. Now, i want to output an HTML list like: 3 4 7 8 9 5 6 Answer Test it. or

PHP append one array to another (not array_push or +)

How to append one array to another without comparing their keys? At the end it should be: Array( [0]=>a [1]=>b [2]=>c [3]=>d ) If I use something like [] or array_push, it will cause one of these results: It just should be something, doing this, but in a more elegant way: Answer array_merge is the elegant way: Doing something like:

Print the keys of an array

I could not figure out how to pass a variable number of variables into a function. I thought passing in an array and using the array keys for the variables names could replace the need to pass extra variables into the function, and it worked (I’m sure there is a better way to accomplish this, suggestions welcome). However, I can’t

Check and return duplicates array php

I would like to check if my array has any duplicates and return the duplicated values in an array. I want this to be as efficient as possible. Example: Also the initial array is always 5 positions long Answer this will be ~100 times faster than array_diff

Advertisement