add 2D array key php [duplicate] - php

This question already has answers here:
PHP foreach change original array values [duplicate]
(5 answers)
Closed 5 years ago.
I tried to add a key and its in a 2D array using a foreach. The problem is that this key isn't "saved". As soon as I try to look the first array, the key I added disappeared.
There is the code :
$Etapes=$this::getEtapes();
foreach($Etapes as $Etape){
$req = $this::getSuiviEtapes();
$Etape['Nom_Suivi'] = $req[0]['Nom_Suivi'];
if($Etape['ID_Etat_Etape']=="22")
{
var_dump($Etape);
var_dump($Etapes);
$this->Etapes=$Etapes;
var_dump($this->Etapes);
}
}
And there is the return
array(3) {
["ID_Etat_Etape"]=>
string(2) "22"
["Nom"]=>
string(36) "Comparatif"
["Nom_Suivi"]=>
string(8) "En_cours"
}
array(2) {
[0]=>
array(2) {
["ID_Etat_Etape"]=>
string(2) "21"
["Nom"]=>
string(12) "Etude"
}
[1]=>
array(2) {
["ID_Etat_Etape"]=>
string(2) "22"
["Nom"]=>
string(36) "Comparatif"
}
}
array(2) {
[0]=>
array(2) {
["ID_Etat_Etape"]=>
string(2) "21"
["Nom"]=>
string(12) "Etude"
}
[1]=>
array(2) {
["ID_Etat_Etape"]=>
string(2) "22"
["Nom"]=>
string(36) "Comparatif"
}
}
as you can see, the "Nom_Suivi" key do not appear in the second and third array.
I don't know if my issue is clear enough. Ask if it isn't.
Thank you for helping.

If you want to modify array you are iterating over with foreach, you either have to use reference - foreach($Etapes as &$Etape) or (prefered way) change the value by using the original array variable and key:
foreach($Etapes as $key => $Etape){
...
$Etapes[$key]['Nom_Suivi'] = $req[0]['Nom_Suivi'];
...
}

Try using
foreach ($fields as $key => $field)
Use the key
Check out this Duplicate of this question

Related

How to get array values from a JSON file in PHP? [duplicate]

This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 3 years ago.
I need to write a foreach using these user's ids:
The array that the API returns:
{"users":[{"id":"14"},{"id":"19"}]}
I want to send a mail based on each user id, thats why I need a foreach statement. How do I do that?
Maybe, here we could first json_decode, then loop through the users and append a username maybe using id values to the array:
$str = '{"users":[{"id":"14"},{"id":"19"}]}';
$array = json_decode($str, true);
foreach ($array["users"] as $key => $value) {
$array["users"][$key]["username"] = "user_" . $value["id"];
}
var_dump($array);
Output
array(1) {
["users"]=>
array(2) {
[0]=>
array(2) {
["id"]=>
string(2) "14"
["username"]=>
string(7) "user_14"
}
[1]=>
array(2) {
["id"]=>
string(2) "19"
["username"]=>
string(7) "user_19"
}
}
}

How to get particular value in the object(WC_Cart) in Woocommerce? [duplicate]

This question already has answers here:
Get cart item name, quantity all details woocommerce
(6 answers)
Closed 4 years ago.
I have The WC()->cart object that has this structure:
object(WC_Cart)#803 (13) {
["cart_contents"]=>
array(3) {
["e369853df766fa44e1ed0ff613f563bd"]=>
array(11) {
["key"]=>
string(32) "e369853df766fa44e1ed0ff613f563bd"
["product_id"]=>
int(34)
["variation_id"]=>
int(0)
["variation"]=>
array(0) {
}
["quantity"]=>
int(1)
["line_tax_data"]=>
array(2) {
["subtotal"]=>
array(1) {
[1]=>
float(31.5)
}
["total"]=>
array(1) {
[1]=>
float(31.5)
}
}
["line_subtotal"]=>
float(150)
["line_subtotal_tax"]=>
float(31.5)
["line_total"]=>
float(150)
["line_tax"]=>
float(31.5)
["data"]=>
object(WC_Product_Simple)#1251 (12) {
After WC()->cart->cart_contents I don't know how to get 'line_subtotal' value.
May I need to convert this object to something else before trying to get any value?
Try this.
foreach(WC()->cart->cart_contents as $cart_content) {
echo $cart_content['line_subtotal']
}
using foreach since the value of "cart_contents" is array.

Trouble getting data from an array in PHP

I've got this array:
array(1) {
["sensory-evaluation"]=> array(6) {
["name"]=> string(18) "Sensory Evaluation"
["value"]=> string(43) "10/22/2015 at 6:00pm | 11/25/2015 at 6:00pm"
["position"]=> string(1) "3"
["is_visible"]=> int(1)
["is_variation"]=> int(1)
["is_taxonomy"]=> int(0)
}
}
I need to be able to get the data from ["value"]. If I do $arr["sensory-evaluation"]["value"] I can get it but the problem is ["sensory-evaluation"] will be different for each element in my array so I need a way to abstract that part but I haven't been able to figure it out.
If you have only one item in the array as you show then:
echo current($arr)['value'];
If you don't know if it exists:
if(isset(current($arr)['value'])) {
echo current($arr)['value'];
}
You could also do:
echo array_values($arr)[0]['value'];
why not use a for each
function getValue($arr){
foreach ($arr as $key => $value) {
var $subArray=$arr[$key];
if( array_key_exists ('value' ,$subArray))
return $arr[$key]['value'];
}
}

Get random id from array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have an array of ids. Right now it has 125 ids in it. I only pasted a few. I would like to select a random id from it and use it in a query. I was using rand_array, but I had a simple array like this: array(1,2,3); with this new array I don't know how to grab a random id.
array(125) {
[0]=>
object(stdClass)#21 (1) {
["id"]=>
string(1) "5"
}
[1]=>
object(stdClass)#22 (1) {
["id"]=>
string(1) "6"
}
[2]=>
object(stdClass)#23 (1) {
["id"]=>
string(1) "7"
}
[3]=>
object(stdClass)#24 (1) {
["id"]=>
string(1) "8"
}
[4]=>
object(stdClass)#25 (1) {
["id"]=>
string(1) "9"
}
[5]=>
object(stdClass)#26 (1) {
["id"]=>
string(2) "10"
}
[6]=>
object(stdClass)#27 (1) {
["id"]=>
string(2) "11"
}
[7]=>
object(stdClass)#28 (1) {
["id"]=>
string(2) "12"
}
}
As #Jack commented under the question, this is probably the easiest way to do what you're trying to do:
echo $array[array_rand($array)]->id;
If you want the IDs for a different purpose, you could extract the IDs from the objects in the array and apply array_rand() on that array:
$idsArr = array(); // Initialize an empty array
foreach ($array as $obj) {
$idsArr[] = $obj->id; // Push each ID into the array
}
$random_id = $array[array_rand($idsArr)]; // Get a random ID from the array
Your new array is two dimensional - you need to specify both ID's to grab an element. So try:
$rand = rand(0,count($array));
$item=$my_array[$rand]['id'];
However I couldn't understand the structure of your array so if you print_r it I can refine this
cheers

How to get last value from this array?

How to get the last array value of this var_dump?
I do a var_dump on a variable ($submission) and get this:
object(stdClass)#148 (8) {
["sid"]=> string(3) "199"
["nid"]=> string(4) "3042"
["submitted"]=> string(10) "1386113448"
["remote_addr"]=> string(9) "127.0.0.1"
["uid"]=> string(2) "21"
["name"]=> string(8) "SClosson"
["is_draft"]=> string(1) "0"
["data"]=> array(1) {
[1]=> array(1) {
[0]=> string(8) "blahblah"
}
}
}
So I need to store blahblah in a variable from the above array, but how?
Thought I could just get it by doing this: $submission['data'][1][0], but that doesn't work. How do I return blahblah from this?
If you need an array, you can type cast it
$result = (array) $submission;
Or as an object, access the data as public properties
echo $submission->data[1][0];
You can use array_pop if you want to get the last value of an array.
http://www.php.net/manual/en/function.array-pop.php

Categories