Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have stored one query result in an php array
Array
(
[0] => stdClass Object
(
[AccountNumber] => 1000000
[FirstName] => james
[LastName] => James Administrator
)
[1] => stdClass Object
(
[AccountNumber] => 1000001
[FirstName] => george
[LastName] => James
)
[2] => stdClass Object
(
[AccountNumber] => 1000002
[FirstName] => Clara
[LastName] => Pradeep
)
[3] => stdClass Object
(
[AccountNumber] => 1000003
[FirstName] => Dona
[LastName] => Kodi
)
)
I needs to print FirstName and LastName for a particular AccountNumber
excepted output is:
FirstName: Clara
LastName: Pradeep
where AccountNumber is 1000002
Thanks in advance
Search through the array for the account number you want, and then output:
$object_array = {your array}; // whatever variable you have to print out what you posted
foreach ($object_array as $account) {
if ($account->AccountNumber == "1000002") { // change the number to a variable if required
echo "<p>FirstName: " . $account->FirstName . "</p>";
echo "<p>:LastName: " . $account->LastName . "</p>";
}
}
Related
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 3 years ago.
Improve this question
I have the following array:
Array (
[ids] => Array (
[0] => e348ae92
[1] => 193ba701
[2] => 58695854
)
[name] => Array (
[0] => Customers
[1] => Suppliers
[2] => Users
)
[subs] => Array (
[0] => 614
[1] => 65
[2] => 99
)
)
I want to take each array key and turn it into a it's own array e.g.
array(
[0] = array(
[0] => e348ae92
[1] => custommers
[2] => 614
)
[1] = array(
[0] => 193ba701
[1] => Suppliers
[2] => 65
)
[2] = array (
[0] => 58695854
[1] => Users
[2] => 99
)
)
I have looked at array_merge, array_combine and a few other things but I have been unsuccessful so far, any pointers in the right direction would be appreciated.
You can use array_map
$f = array_map(null, $a['ids'], $a['name'], $a['subs']);
print_r($f);
Working example:https://3v4l.org/dDG0Y-
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 3 years ago.
Improve this question
i have this multidimensional array.
Array
(
[0] => Array
(
[firstName] => Hadi
[lastName] => Pratama
[age] => 22
)
[1] => Array
(
[firstName] => Jefri
[lastName] => Ronaldo
[age] => 21
)
[2] => Array
(
[firstName] => Rizky
[lastName] => Aulia
[age] => 20
)
)
i want to put them into indexed array like this.
Array = ({"firstName":"Hadi","lastName":"Pratama","age":"22"},{"firstName":"Jefri","lastName":"Ronaldo","age":"21"},
{"firstName":"Rizky","lastName":"Aulia","age":"20"});
help me please.
Not sure quite what your question is here (please clarify by editing your post).
Your output does not look like a Php array. More like JSON.
When you say remove indexes, could this be from having non sequential 0 counted indexes?
See below:
<?php
$people =
[
[
'name'=>'fred',
'family'=>'flintstones'
],
[
'name'=>'barney',
'family' => 'rubble'
],
[
'name'=> 'wilma',
'family' => 'flintstones'
]
];
var_dump(json_encode($people));
unset($people[1]);
var_dump(json_encode($people));
var_dump(json_encode(array_values($people)));
Output:
string(116) "[{"name":"fred","family":"flintstones"},{"name":"barney","family":"rubble"},{"name":"wilma","family":"flintstones"}]"
string(88) "{"0":{"name":"fred","family":"flintstones"},"2":{"name":"wilma","family":"flintstones"}}"
string(80) "[{"name":"fred","family":"flintstones"},{"name":"wilma","family":"flintstones"}]"
You can use array_values to reindex arrays before encoding to JSON to 'remove' those indexes, and turn into a list of objects.
json_encode() : Returns the JSON representation of a value
echo json_encode($a);
Working example :- https://3v4l.org/EgOtE
Question
Your array already has the correct dimensions, just run json_encode($array); over it and you will have the result you're looking for.
If you want to use an associative array, you'll have to be a bit more creative:
$result = [];
foreach($array as $index) {
$result[$index['firstName']] = $index;
}
var_dump($result);
Result:
Array
(
[Hadi] => Array
(
[firstName] => Hadi
[lastName] => Pratama
[age] => 22
)
[Jefri] => Array
(
[firstName] => Jefri
[lastName] => Ronaldo
[age] => 21
)
[Rizky] => Array
(
[firstName] => Rizky
[lastName] => Aulia
[age] => 20
)
)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
In the following script
<?php
$people = array("Joe", "Pete", "Mike", "Mark");
print_r (each($people));
?>
if the 3rd line was reiterated?
Input:
$people = array("Joe", "Pete", "Mike", "Mark");
print_r(each($people));
print_r(each($people));
print_r(each($people));
print_r(each($people));
var_dump(each($people));
Output:
Array
(
[1] => Joe
[value] => Joe
[0] => 0
[key] => 0
)
Array
(
[1] => Pete
[value] => Pete
[0] => 1
[key] => 1
)
Array
(
[1] => Mike
[value] => Mike
[0] => 2
[key] => 2
)
Array
(
[1] => Mark
[value] => Mark
[0] => 3
[key] => 3
)
bool(false)
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 6 years ago.
Improve this question
I need array merge with value concat if array title is same name. my array print is->
Array
(
[0] => Array
(
[id] => 7867867
[title] => Title1
)
[1] => Array
(
[id] => 3452342
[title] => Title2
)
[2] => Array
(
[id] => 1231233
[title] => Title2
)
[3] => Array
(
[id] => 5867867
[title] => Title1
)
[4] => Array
(
[id] => 7867777
[title] => Title1
)
)
and i want to this format like if title is same concat the array value in one array and other array is removing.
like that->
Array
(
[0] => Array
(
[id] => 7867867,5867867,7867777
[title] => Title1,Title1,Title1
)
[1] => Array
(
[id] => 3452342,1231233
[title] => Title2,Title2
)
)
If you know that how to solve it please help me!
Thanks
Try this,
foreach($array as $val)
{
$titlearray[] = $val['title'];
}
$titlearray = (array_unique($titlearray));
//print_r($titlearray);
foreach($array as $val)
{
$key = array_search($val['title'], $titlearray);
$newarray[$key]['id'][] = $val['id'];
$newarray[$key]['title'][] = $val['title'];
}
DEMO
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Our Result:
Array (
[0] => Array ( [0] => Province [1] => Party [2] => Age [3] => Name [4] => Gender )
[1] => Array ( [0] => Quebec [1] => NDP [2] => 22 [3] => Liu, Laurin [4] => Female )
[2] => Array ( [0] => Quebec [1] => Bloc Quebecois [2] => 22 [3] => Mourani, Maria [4] => Female )
)
I want a result looking like this: How to convert like this?
array(
['Province'=>'Quebec','Party'=>'NDP','Age'=>22,'Name'=>'Liu, Laurin','Gender'=>'Female'],
['Province'=>'Quebec','Party'=>'Bloc Quebecois','Age'=>43,'Name'=>'Mourani, Maria','Gender'=>'Female']
)
OR
array(
['Province', 'Party', 'Age', 'Name', 'Gender'],
['Quebec', 'NDP', 22, 'Liu, Laurin', 'Female'],
['Quebec', 'Bloc Quebecois', 43, 'Mourani, Maria', 'Female']
)
Pretty simple using an array_combine() to set the headers for each row, and just walking the array setting those headers:
$headers = array_shift($myArray);
array_walk(
$myArray,
function(&$row) use ($headers) {
$row = array_combine($headers, $row);
}
);