Take value from array which is in array [duplicate] - php

This question already has answers here:
PHP - Accessing Multidimensional Array Values
(4 answers)
Closed 8 years ago.
I've got this url:
http://web.com/script.php?identifiers%5Bmc%5D%5Bnick%5D=name1&identifiers%5Bcs%5D%5Bnick%5D=name2&identifiers%5Bcs%5D%5Bpassword%5D=mypass
so i will get array like this:
[identifiers] => Array
(
[mc] => Array
(
[nick] => name1
)
[cs] => Array
(
[nick] => name2
[password] => mypass
)
)
How do I take value name1 and put into variable $mc_name?

That's a simple array containing another array so you can simply specify multiple indexes for included array:
$mc_name = $_GET['identifiers']['mc']['nick'];
To better understand how it works think of it like assigning each array first to a variable like:
$identifiers = $_GET['identifiers'];
$mc_array = $identifiers['mc'];
$mc_name = $mc_array['nick'];
which will essentially do the same thing at once, without the need to specify multiple variables and arrays.

Start with:
identifiers = $_GET['identifiers']
If you know the key names, then simply:
$mc_name = $identifiers['mc']['nick']
If you know it's the first value or the first value, then you can:
$mc_name = array_shift($identifiers); // get the 'mc' array
$mc_name = array_shift($identifiers); // get the 'nick' value
Not that array_shift will actually remove the elements from the original array.

Related

How to use array_map with reset callback in php 8 [duplicate]

This question already has answers here:
Change the array KEY to a value from sub array
(7 answers)
How can i get sub array value to main array key? [Php] [duplicate]
(2 answers)
Multidimensional indexed array to associative array depending on column value
(2 answers)
Closed 10 months ago.
I have a function that work good in php 7 but in php 8.0.11 has warning
$orders->result = array_map('reset', $orders->result);
E_WARNING: reset(): Argument #1 ($array) must be passed by reference, value given in
Of course, I can use this code instead that but maybe slower than array_map
foreach ($orders->result as $item)
$result[$item[0]['order_id']] = $item[0];
Edit:
before foreach or array_map the output like this
Array
(
[0] => Array
(
[order_id] => 41111909
[shop_id] => 34277
[user_id] => 42363
[status_id] => 4
)
)
after use foreach output like this
Array
(
[41111909] => Array
(
[order_id] => 41111909
[shop_id] => 34277
[user_id] => 42363
[status_id] => 4
)
)
How to solve it ?
The simplest replacement is to make an arrow function (single-expression closure) to get the element for you.
If the arrays are plain lists, so that you're always getting element 0, that's as simple as:
$orders->result = array_map(fn($item) => $item[0], $orders->result);
Another alternative in this case is array_column:
$orders->result = array_colum($orders->result, 0);
If you have other keys, you could use either reset or array_key_first in the callback instead:
$orders->result = array_map(fn($item) => $item[array_key_first($item)], $orders->result);
(I suspect array_key_first will be slightly more efficient, since it doesn't need to manipulate the internal array pointer.)
It's not entirely clear what you're trying to do, but it seems like you are trying to get the first value from nested arrays. You should be able to use a function:
$orders->result = array_map(function($v) { return reset($v); }, $orders->result);
You may also try using current if it does what you need as it doesn't take a reference and the array pointer should already be reset by array_map:
$orders->result = array_map('current', $orders->result);
After the edit the code in question is not re-indexing the array as you show in the before and after. To do that is simple:
$orders->result = array_column($orders->result, null, 'order_id');

How to retain the index 0 when using unset in removing index [duplicate]

This question already has answers here:
How to rearrange the array element from 0th index in the sequence to end?
(1 answer)
How to reindex an array?
(6 answers)
Closed 2 years ago.
I have a session variable like this:
$list = Session::get('list_id')
the $list variable value has a value of Array ( [0] => A, [1] => B,[2] => C, [4] => D)
when I am using unset($list [0]);
I am getting a value of Array ([1] => B,[2] => C, [4] => D)
The problem that I am getting is all of my parameters has a parameters of $list[0] I need the index 0 always, so that value that I am looking for after I remove the index 0 is Array ([0] => B,[1] => C, [2] => D)
so that I can store it again on my session.
is there a way to do this?
Use array_values function if you are sure your array has only numerical index keys.
See this link for more details; https://www.php.net/manual/en/function.array-values.php
Do not use unset.
array_shift — Shift an element off the beginning of array
$removedElement = array_shift($yourArray);
array_shift() shifts the first value of the array off and returns it, shortening the array by one element and moving everything down.

Collect all values from nested array row of specific key [duplicate]

This question already has answers here:
How to get an array of specific "key" in multidimensional array without looping [duplicate]
(4 answers)
Closed 1 year ago.
Need to create a list that consists of all values stored in the array row of a specific key (product_id). Currently, doing a print_r of my $bestsellers variable produces the following array:
Array
(
[0] => stdClass Object
(
[product_id] => 178
[order_item_qty] => 9
)
[1] => stdClass Object
(
[product_id] => 233
[order_item_qty] => 4
)
[2] => stdClass Object
(
[product_id] => 179
[order_item_qty] => 1
)
)
Other SO answers led me to try:
$ids = array_column($bestsellers, 'product_id');
...but that produced an empty array, I guess because the row I’m trying to grab is nested in there? With that in mind I tried
foreach($bestsellers as $bestseller) {
$ids = array_column($bestsellers, 'product_id');
}
...which produced no result at all.
Hopefully someone can help clue me in as to where I’m going wrong. Thanks!
The nested values are objects, not arrays (can't you see stdClass Object in the output?). array_column is for 2-dimensional arrays. You need to access the properties using object syntax.
$ids = array_map(function($x) { return $x->product_id; }, $bestsellers);
For future reference, array_column will work for this in PHP 7, so you must be using PHP 5.
For PHP 7, your code
$ids = array_column($bestsellers, 'product_id');
will do what you want it to.
See the difference here on 3v4l.org.

get value from array with std object (php) [duplicate]

This question already has answers here:
stdClass object array inside of another array
(3 answers)
Closed 5 years ago.
my code in PHP :
$stuff = array($this->complaint_model->get_reply($id)->result());
print_r($stuff);
result :
Array (
[0] => Array (
[0] => stdClass Object (
[id_customer] => 21
[id_complaint] => 2
[id] => 4
[nama_customer] => Muhammad Bima Zehansyah
[from] => Admin
[balasan] => coba update
)
)
)
my question is , how to get value [nama_customer] ? thx before guys
Try this
$stuff = array($this->complaint_model->get_reply($id)->result());
echo $stuffVal = $stuff[0][0]->nama_customer;
Get the value like this
$stuff[0][0]->nama_customer
Here you have multidimensional array object that's why you need to first Travers two array like $stuff[0][0] and then the object like $stuff[0][0]->nama_customer
Actually you do not need to put the result into additional array and it would be wise to check if the result is not empty.
So you just take a first item from your result (which is an object) and call the parameter nama_customer
$stuff = $this->complaint_model->get_reply($id)->result();
if (!empty($stuff[0]))
echo $stuff[0]->nama_customer;

In PHP, is it possible to get a 1 dimmensional array using PDO? [duplicate]

This question already has answers here:
PDO fetchAll array to one dimensional
(3 answers)
Closed 3 years ago.
This seems like it should be (and probably is) trivial. I have a simple query:
SELECT Name From User;
When I run the query using this code:
$rows = $preparedStatement->fetchAll(PDO::FETCH_ASSOC);
$Rows looks like this:
Array ( [0] => Array ( [Name] => Doug ) [1] => Array ( [Name] => John ) )
Is there an easy way to make the array look something like this:
Array( Doug, John)
Using the constant PDO::FETCH_COLUMN:
$columnNumber = 0;
$rows = $preparedStatement->fetchAll(PDO::FETCH_COLUMN, $columnNumber);
This way you'll get exactly what you proposed.
You can also do the following:
$columnNumber = 0;
$rows = $preparedStatement->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_UNIQUE, $columnNumber);
This way you'll get an array with unique values.
Source: http://www.php.net/manual/en/pdostatement.fetchall.php
I think the right answer has been given by Jaison Erick, in case you need to flatten what you've got returned (not really recommended), this would do it:
$flat = reset((call_user_func_array('array_merge_recursive', $rows)));

Categories