I have a section of an array defined:
$arrData['categories'] = array();
array_push($arrData['categories'],array("category" => array(array("label"=>"Beef"),array("label"=>"Chicken"))));
array_push($arrDAta['categories'][0]['category'],array("label"=>"pork"));
The arrays are nested so that it encodes into JSON properly with all the {[{[{[]}]}]} formatted properly.
However, I need to create these types of meats dynamically, not statically. When I try to add onto the array in the third line of code to simulate dynamic creation, it throws an error:
Warning: array_push() expects parameter 1 to be an array, null given
Well, $arrData['categories'][0]['category] IS an array, so why does it shoot me down?
To show myself that I'm not crazy, I var_dump $arrData['categories'][0]['category'] and get an array of size 1:
array (size=1)
'category' =>
array (size=2)
0 =>
array (size=1)
'label' => string 'Beef' (length=4)
1 =>
array (size=1)
'label' => string 'Chicken' (length=7)
Its the capitalization. In the third line the variable $arrDAta is capitalized differently than the others ($arrData)
Related
I am trying to array_merge id’s from one array into another, it’s merging but putting the new values on a new array line:
IDs to be added: $attachment_ids
array (size=2)
0 => string '2620' (length=4)
1 => string '2621' (length=4)
IDs which already exist and will be added to: $existing_data
array (size=1)
0 => string '2589,2561,2432,2422' (length=19)
result of my array_merge:
$merged_data = array_merge($attachment_ids, $existing_data);
is
array (size=3)
0 => string '2620' (length=4)
1 => string '2621' (length=4)
2 => string '2589,2561,2432,2422' (length=19)
My expected result is:
array (size=1)
0 => string '2620,2621,2589,2561,2432,2422'
If you just need the 1 string with all of the values, you could use implode() with the result you have so far...
$merged_data = [ implode(",", $merged_data) ];
Shortly array merge with array_map
$result=array_map(null,$array1,$array2);
array_merge() requires 2 arrays. The existing data is all in a string. With explode, this data can be converted into an array. With implode() a list is created after array_merge().
$attachment_ids = ['2620','2621'];
$existing_data = ['2589,2561,2432,2422'];
$existing_data = explode(',',$existing_data[0]);
$result = [implode(',',array_merge($attachment_ids,$existing_data))];
//$result: array(1) { [0]=> string(29) "2620,2621,2589,2561,2432,2422" }
Update
Alternatively, strings can be chained manually.
$result = [implode(',',$attachment_ids).','.$existing_data[0]];
I have the following define in my code
define('SERVICE', 1);
When I now initialize an array like this
$serviceLimit[SERVICE_PAGECHECK][0] = 0;
and now do a var_dump on $serviceLimit in my mind it should output it like this
array (size=1)
'0' =>
array (size=1)
0 => int 0
However, it currently looks like this
array (size=1)
'SERVICE' =>
array (size=1)
0 => int 0
How can this be? Why is the array index using the name of the variable rather than the value?
Your code seems strange, because of using 2 different constants: SERVICE and SERVICE_PAGECHECK. Just correct the constant name.
I have an array like the example below (from var_dump($tagged);:
array (size=1)
0 =>
array (size=7)
0 => string '#raining' (length=8)
1 => string '#raining' (length=8)
2 => string '#driving' (length=8)
3 => string '#hungrySoon' (length=11)
4 => string '#strangeworld' (length=13)
5 => string '#fruitweekFunky' (length=15)
6 => string '#kevins_rainbow_disco' (length=21)
7 => string '#raining' (length=8)
8 => string '#fruitweekFunky' (length=15)
I am simply after displaying the array as follows (From the most frequent first):
#raining
#fruitweekFunky
#driving ...etc
To achieve exactly what you've asked for (a descending ordered array of the highest occurences to the lowest) step by step:
Count the number of occurences:
$occurences = array_count_values($tagged[0]);
Sort the array (by value, because the number of occurrences is the current array value and the tag is the key - arsort() maintains the original keys):
arsort($occurences);
Get array of the keys for output (because the tags are currently keys):
var_dump(array_keys($occurences));
Fact is, $tagged is an array of arrays. So you need to take its first element.
Try :
$result = array_count_values(array_values($tagged[0]));
var_dump($result);
I have this array of arrays
array (size=2)
'login' =>
array (size=3)
20 =>
array (size=1)
0 =>
object(File)[3]
10 =>
array (size=1)
0 =>
object(Database)[4]
5 =>
array (size=1)
0 =>
object(Closure)[5]
I'm trying to reorder the position of the keys in the second level arrays (where it says 20, 10, 5) so the result should be
array (size=2)
'login' =>
array (size=3)
5 =>
array (size=1)
0 =>
object(File)[3]
10 =>
array (size=1)
0 =>
object(Database)[4]
20 =>
array (size=1)
0 =>
object(Closure)[5]
The problem is that I can't figure out how to do that so please help
You need to do
ksort($array['login']);
This will sort the array keys so they are ascending. If you wanted them descending you would do
krsort($array['login']);
PHP has lots of handy functions for sorting arrays
You could have a look at Array Sorting and find out, that 'ksort' is your choice ;-)
Try PHPs ksort function on your second level array: http://php.net/ksort
It will sort your array by key while maintaining the key-data-associations
PHP has a variety of array sorting functions.
In your case, the most appropriate would seem to be ksort(), which is summarised thus:
Sorts an array by key, maintaining key to data correlations.
As pointed out in the comments, your particular example is already in the reverse of the required order; if this can be guaranteed, you could also use array_reverse(), being sure to pass the optional $preserve_keys parameter as true.
Specifically, you want to re-order the sub-array with the key 'login'; assuming your overall array is called $data, you would want to write this:
ksort($data['login']);
// note no need for an assignment, as PHP's sort functions act in-place
or this:
$data['login'] = array_reverse($data['login'], true);
// assign back to the original variable, as array_reverse() returns a new array
I am new to php. Please let me know how do I count number of array entry in under array 80cb936e55e5225cd2af.
array
'status' => int 1
'msg' => string '2 out of 1 Transactions Fetched Successfully' (length=44)
'transaction_details' =>
array
'80cb936e55e5225cd2af' =>
array
0 =>
array
...
1 =>
array
...
Assuming your array variable is $arr : ...
$count = count($arr['transaction_details']['80cb936e55e5225cd2af']);
(But this will only count the indexes/integers of that sub-array, not the values inside them!)
You can use PHP's count() like this
$noOfEntries = count($array['transaction_details']['80cb936e55e5225cd2af']);
This will get you the number of arrays inside 80cb936e55e5225cd2af (not the overall amount of elements).