Read Multi Dimension Array in PHP [duplicate] - php

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
how to read "test" in "dir"?
$_USERS = array (
10 => array (
`name` => `xyz`,
`role` => `user`,
`dir` => `["test","test1"]`,),
);

Assuming you are creating this array statically in your script, I would advise against using the ` quotation. Instead use a single or double quotation.
The below example works the way I believe you are requesting:
$_USERS = array (
10 => array (
"name" => 'xyz',
"role" => 'user',
"dir" => '["test","test1"]',),
);
echo json_decode($_USERS[10]["dir"])[0];
Because the array in "dir" is not a real array, we can translate it into a PHP array by using json_decode.

Related

Add Key and Value to a multidimesnsion array PHP [duplicate]

This question already has answers here:
Add a static value to each row in a 2d array
(3 answers)
php - Add value with key to all the elements [duplicate]
(1 answer)
How can I push single element in existing array of JSON? [duplicate]
(5 answers)
Closed 11 days ago.
I have the following multidimenson array
array (
'count' => 386,
'report' =>
array (
'uuid' => '183a3956-9425-43da-845c-2839c30a951b',
'name' => 'OnlyScrumFND',
'Have' =>
array (
0 =>
array (
'uuid' => '00ad6013-4109-4940-a711-4f8fb5389e8c',
),
1 =>
array (
'uuid' => 'd651a86d-beac-498a-85a0-75ce62f28f4e',
),
),
),
)
I would like to add some info to the Array in a sublevel
foreach ($OUTPUT AS &$have['report']['Have']) {
$have['name'] = "something";
}
But it is not woking, Any hint? thanks rob
You are very close. you can try with following solution.
foreach ($OUTPUT['report']['Have'] as &$item) {
$item['name'] = "something";
}
I am using & operator here to modify the original array element in foreach loop instead of copied one.
Find the implementation below .
you can check here

PHP Get array of a property from array of objects [duplicate]

This question already has answers here:
How to extract and access data from JSON with PHP?
(1 answer)
PHP JSON Specific Key To Array [duplicate]
(3 answers)
Closed 2 years ago.
I have an API that sends me the data in the format
[{"part_no":"AAA"},{"part_no":"BBB"},{"part_no":"CCC"},......{"part_no":"ZZZ"}]
I want to create an array like ["AAA", "BBB", ...., "ZZZ"] from the above array.
I know it's possible by iterating the array item by item, and appending it to a new array, but thought that there might be a better (and hopefully faster) approach.
Like C# hasLinq that does this all in a one-liner, JS has map, it is possible to do a similar thing in PHP ?
<?php
$json = '[{"part_no":"AAA"},{"part_no":"BBB"},{"part_no":"CCC"},{"part_no":"ZZZ"}]';
$data = json_decode($json, true);
$result = array_column($data, 'part_no');
var_export($result);
Output:
array (
0 => 'AAA',
1 => 'BBB',
2 => 'CCC',
3 => 'ZZZ',
)

Get array of user IDs from multi-dimensional array [duplicate]

This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 5 years ago.
I've the following array:
Array
(
[0] => Array
(
[ID] => 1
[more_user_data] => More
)
[1] => Array
(
[ID] => 2
[more_user_data] => More
)
)
Now I want to have a comma separated list of the IDs to use them in an own array. To get something like this:
array(1,2)
How could I only extract the IDs from the second array?
Use array_column() function like:
$arr = array_column($array, 'ID');
Working Example

Take value from array which is in array [duplicate]

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.

Key value in PHP from URL [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to explode URL parameter list string into paired [key] => [value] Array?
Quick question
I have a URL sting that I wish to output the Key value as an array eg
$url ="www.domain.com?test=1&test2=2&test3=3";
and wish to have the output as an array
key => value so I can call any of the keys
eg
array (
test => 1,
test1 => 2,
test2 => 3,
)
cant use explode &
Just thinking do i have to do a loop and match between & and = for the key
I would use parse_url() with parse_str():
$url = parse_url('www.domain.com?test=1&test2=2&test3=3');
parse_str($url['query'], $keyvalue);
var_dump($keyvalue);
$keyvalue should contain your desired array.

Categories