Add new array at the beginning of multidimensional array [duplicate] - php

This question already has answers here:
How to insert an item at the beginning of an array in PHP?
(8 answers)
Closed 8 years ago.
I have a multidimensional array like this:
// Create an empty array
$noti_log = array();
// Add first array within a multidimensional array
$noti_log = array(
array('hello world')
);
And then, when I want to add a new array inside, I do:
$noti_log[] = array('Greetings');
The problem is that whenever I add a new array, it ends up underneath the old ones. Instead, I want new arrays to line up on top of the old ones.
So in this case, array('Greetings') should be on top of array('hello world')...
How do I add new arrays that land on top?

Use array_unshift():
array_unshift() prepends passed elements to the front of the array. Note that the list of elements is prepended as a whole, so that the prepended elements stay in the same order. All numerical array keys will be modified to start counting from zero while literal keys won't be touched.
array_unshift($noti_log, array('Greetings'));

Related

Separate the key from the value [duplicate]

This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 3 years ago.
I set up a new site and want a workaround for the problem.
I have this text in the mysql table
{"200":"5","220":"0","65":"4","80":"0","199":"1","197":"1","198":"0","257":"4","223":"0"}
I want to fetch the ID and its value by php
For example
$id[200] = 5
You can use json_decode() to convert JSON string into an array. Since your JSON string contains numeric indexes so you need to use curly brackets to fetch the values
$str = '{"200":"5","220":"0","65":"4","80":"0","199":"1","197":"1","198":"0","257":"4","223":"0"}';
$array = json_decode($str);
echo $array->{'200'};
Or you can add second parameter in json_decode as true to fetch the value as normal array
$array = json_decode($str,true);
echo $array[200];
Here is the live demo

Imploding specific associative array key values [duplicate]

This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
I've got associative array with results from database containing data like in following structure:
$arr[0] = Array("id"=>4, "otherdata"=>"something");
$arr[1] = Array("id"=>6, "otherdata"=>"something else");
$arr[2] = Array("id"=>15, "otherdata"=>"something totally different");
I would like to implode data that is only in id key for each $arr entry, so that final imploded string is 4,6,15 (gluded with ,).
Right now I've got some solutions:
Doing it in pure PHP within Smarty.
Creating function that will implode result from array_map which creates new table with id's only.
Assigning variable within Smarty template and creating implode-like result string with foreach.
but neither of them I am happy of.
Is there any other simple way to achieve desired result?
The 4th solution:
echo implode(',', array_column($arr, 'id'));

Set Array Equal to Another Array without Specified Key/Values [duplicate]

This question already has answers here:
php array difference against keys of an array and an array of keys?
(3 answers)
Unset elements using array keys
(2 answers)
Closed 5 years ago.
I am attempting to mirror the behavior of newArray = oldArray, with the caveat of excluding some key/values of the oldArray, so something like newArray = oldArray - undesiredOldKeyValue. I realize this is fully doable with a foreach on the oldArray and using an if to see if the encountered key is desired or not, but I am interested in a simpler or more concise approach if possible.
A couple of things to keep in mind, I need to exclude key/value pairs based on key, not value. I do not want to modify the oldArray in the process of doing this.
You may try to use array_filer. Something like:
$new_array = array_filter($old_array, function ($value, $key) {
// return false if you don't want a value, true if you want it.
// Example 1: `return $value != 'do not keep this one';`
// Example 2: `return !in_array($key, ['unwanted-key1', 'unwanted-key2', 'etc']);`
}, ARRAY_FILTER_USE_BOTH);
It will filters elements of an array using a callback function.

Get nth key of associative php array [duplicate]

This question already has answers here:
Accessing an associative array by integer index in PHP
(6 answers)
Closed 7 years ago.
I want to get the value of the KEY of an associative PHP array at a specific entry. Specifically, I know the KEY I need is the key to the second entry in the array.
Example:
$array = array('customer' => 'Joe', 'phone' => '555-555-5555');
What I'm building is super-dynamic, so I do NOT know the second entry will be 'phone'. Is there an easy way to grab it?
In short, (I know it doesn't work, but...) I'm looking for something functionally equivalent to: key($array[1]);
array_keys produces a numerical array of an array's keys.
$keys = array_keys($array);
$key = $keys[1];
If you're using PHP 5.4 or above, you can use a short-hand notation:
$key = array_keys($array)[1];

PHP using a string with multiple child keys as a lookup key for an associated array [duplicate]

This question already has answers here:
String to multidimensional array path
(1 answer)
Nested numbering to array keys
(1 answer)
Closed 8 years ago.
I would like a multiple child 'key' as a 'lookup' to get a object out of various associative arrays. For example:
$lookup_key = "['objects'][0]['object2']";
// this will be stored so when I need to get an object's value from various different arrays I can use these string to form a lookup key to get certain objects
$object_array= array();
$object_array= ["objects"=>[["object1"=>"boot", "object2"=>"shoe"],"object"], "flowers"];
// using that key get the value
$object_value= $object_array->lookup($lookup_key);
Now does php have a method that already does this type of lookup or I suppose its an multidimensional key?
Any help would be great! Thanks in advance. This is part of an object lookup table.
I approached this in a slightly different way by storing a string of the objects then exploding that into an array from that travelling across that array to get the value:
$object_array= array();
$object_array= ["objects"=>[["object1"=>"boot", "object2"=>"shoe"],"object"], "flowers"];
$lookup=array();
$lookup_key="objects,0,object2";
$lookup=explode(',',$lookup_key);
$temp_object=array();
$new_object_array=array();
$new_object_array=$object_array;
$count=count($lookup);
for($i=0; $i<$count; $i++) {
$temp_object=$new_object_array[$lookup[$i]];
$new_object_array=$temp_object;
}
echo "\n value: $new_object_array";
As I was search for a quick implementation this seemed to be the best way within my time constraints and php lose variable casting.

Categories