PHP: Replace Array Index Keys with Existing Unique Values - php

I queried my database and it stores the results into an array. It looks like this:
Array
(
[0] => Array
(
[Submission_ID] => 111
[First_Name] => Dylan
[Last_Name] => Taylor
[Abstract_Title] => Research 1
)
[1] => Array
(
[Submission_ID] => 222
[First_Name] => Michael
[Last_Name] => Jones
[Abstract_Title] => Research 2
)
[2] => Array
(
[Submission_ID] => 333
[First_Name] => Wills
[Last_Name] => Adams
[Abstract_Title] => Research 3
)
)
This is all placed in a variable called $results. Currently, I'm displaying bits of data on my page like so:
echo $results[0][Abstract_Title]
It all works, however, it be convenient if the index keys were replaced with the Submission ID:
Array
(
[111] => Array
(
[Submission_ID] => 111
[First_Name] => Dylan
[Last_Name] => Taylor
[Abstract_Title] => Research 1
)
[222] => Array
(
[Submission_ID] => 222
[First_Name] => Michael
[Last_Name] => Jones
[Abstract_Title] => Research 2
)
[333] => Array
(
[Submission_ID] => 333
[First_Name] => Wills
[Last_Name] => Adams
[Abstract_Title] => Research 3
)
)
So I can do this instead (otherwise I would have to print the array just to look up the index key every time):
echo $results[111][Abstract_Title]
Any ideas? (I'm new to programming.) The closest answer I've found is: php replace array id keys but I can't get my head around the solution. I figured a forloop is the best option? Should the new array be placed in the same variable or new variable?

You can simply use array_combine and array_column like as
$final_result = array_combine(array_column($result,'Submission_ID'),$result);
print_r($final_result);

Related

Extract single index value (from every sub-array) from a multi-Key multidimensional array [duplicate]

This question already has answers here:
Can I get all keys of an multi level associative arrays in php
(5 answers)
Closed last year.
I am trying to create an single-dimension array of 'id's' extracted from a multidimensional array that will vary in depth. I need to extract the value from EACH array (no matter how deep). Each array has been indexed (see below) with the same keys. I have tried flattening so I could use 'array_column' (doesn't work because of the number of keys in each array), as well as methods like print_r(array_keys($data[0])[0]) (doesn't work for unknown dimension depth). It seems simple enough but I'm not finding any examples that are like this. Any direction is appreciated. Thank you.
Array
(
[0] => Array
(
[id] => 1000005
[first_name] => James
[last_name] => Smith
[position_root] => CHF CUST EX
[position_area] => Customer Operations
[items] => Array
(
[0] => Array
(
[id] => 1000134
[first_name] => Brandt
[last_name] => Jones
[position_root] => BS APL PJCTS
[position_area] => Customer Executive Support
[items] => Array
(
)
)
[1] => Array
(
[id] => 1000149
[first_name] => Daniel
[last_name] => Brown
[position_root] => CUST PROG
[position_area] => CUSTOMER PROGRAMS
[items] => Array
(
[0] => Array
(
[id] => 1000060
[first_name] => Duane
[last_name] => Pearson
[position_root] => CUST PRG IN
[position_area] => Customer Program Design
[items] => Array
(
)
)
What I am hoping for is:
[0] => 1000005
[1] => 1000134
[2] => 1000149
[3] => 1000060
... and so on ...
As suggested, you can use array_walk_recursive to achieve this.
<?php
$ids = [];
array_walk_recursive($data,function($value,$key) use (&$ids){
if($key == 'id') $ids[] = $value;
});
print_r($ids);

How to add a new key value pair to each object in a PHP multi-dimensional array?

I need to map over an existing multi-dimensional array and conditionally add a new item to each array object.
If the original Array is :
Array
(
[0] => Array
(
[id] => 4
[uid] => 1
[name] => Dave Spicer
[content] => another post
[post_date] => 2018-12-03 00:02:26
)
[1] => Array
(
[id] => 3
[uid] => 2
[name] => John Doe
[content] => some post
[post_date] => 2018-12-03 00:02:21
)
[2] => Array
(
[id] => 1
[uid] => 1
[name] => Dave Spicer
[content] => My first post!
[post_date] => 2018-12-02 23:21:07
)
)
I'd like to loop through it, conditionally adding a new key value pair to each array object which would result in a new array like this :
Array
(
[0] => Array
(
[id] => 4
[uid] => 1
[name] => Dave Spicer
[content] => another post
[post_date] => 2018-12-03 00:02:26
[liked] => YES
)
[1] => Array
(
[id] => 3
[uid] => 2
[name] => John Doe
[content] => some post
[post_date] => 2018-12-03 00:02:21
[liked] => NO
)
[2] => Array
(
[id] => 1
[uid] => 1
[name] => Dave Spicer
[content] => My first post!
[post_date] => 2018-12-02 23:21:07
[liked] => YES
)
)
I assume that I need to create a new array, and that it isn't possible to push into a existing multi-dimensional array - as that's what I've been trying to do to no avail - but I could be completely wrong there. Please let me know if that is the case.
I've tried copying the array and running a foreach loop on it but have only been able to add one new key value pair at the very end of all of the arrays as a new array object.
Could somebody point me in the direction of the correct function(s) to be using here please? array_merge? array_combine? array_push? etc
You add an element to an array simply by assigning to the index.
foreach ($array as $i => $element) {
$array[$i]['liked'] = get_like($element);
}
You can also use a reference variable in the foreach loop:
foreach ($array as $i => &$element) {
$element['liked'] = get_like($element);
}

Remove the parent array key in PHP?

I have an array structure like this :
Array (
[donate] => Array
(
[amount_other] => 222
[pay_method] => Array
(
[5] => Array
(
[first_name] => sam
[last_name] => indi
[cc_type] => mc
[cc_number] => 5123456789012346
[cc_ccv2] => 111
[cc_exp_month] => 10
[cc_exp_year] => 20
)
)
[notes] => Test comment.
)
)
I want to remove key [5] from the array, so that the new array becomes :
Array
(
[donate] => Array
(
[amount_other] => 222
[pay_method] => Array
(
[first_name] => sam
[last_name] => indi
[cc_type] => mc
[cc_number] => 5123456789012346
[cc_ccv2] => 111
[cc_exp_month] => 10
[cc_exp_year] => 20
)
[notes] => Test comment.
)
)
I want this because the array key changes and I want to access the inner array directly so that I don't have to change the key each time in the code. If there are other ways to achieve this.. Please help.
Thanks in advance.
$array['donate']['pay_method'] = current($array['donate']['pay_method']);
$array['donate']['pay_method'] = array_shift($array['donate']['pay_method']);

PHP: Accessing child arrays when you don't know the parent's keys and you can't just search for the value needed

I have a PHP array containing movie descriptions ($descriptions) and another PHP array containing showtimes and ticket quantities ($stock) for those movies. For each unique movie ID in $descripctions, I need to get all of the showtimes and tickets from $stock.
Is there no built-in PHP function that will search for the unique ID and return the chain of keys needed to access that value? I can't find an elegant solution after a full day of Googling and reviewing every array function in the PHP manual twice.
If there's no more elegant solution, is my best approach a custom recursive function? If so, any guidance on best approach / control structure? Maybe counting array elements and using several levels of while loops to go through each level of the array?
Here's the beginning of the $stock array I'm trying to get the data from -- won't post the entire print_r() because the source array is over 67,000 lines long but this much should indicate the structure I'm working with.
Thank you very much!!!
Array (
[products] => Array (
[venue] => Array (
[0] => Array (
[title] => 3-D Digital Cinema
[length] => 25
[memberonly] => 0
[shows] => Array (
[show] => Array (
[0] => Array (
[title] => Planet You
[location] => 3-D Digital Cinema
[eventnumber] => 521
[date] => Array (
[0] => Array (
[eventdate] => 2012/01/01
[times] => Array (
[time] => Array (
[0] => Array (
[starttime] => 13:00:00
[instock] => 110
)
[1] => Array (
[starttime] => 16:00:00
[instock] => 110
)
)
)
)
[1] => Array (
[eventdate] => 2012/01/02
[times] => Array (
[time] => Array (
[0] => Array (
[starttime] => 13:00:00
[instock] => 110
)
[1] => Array (
[starttime] => 16:00:00
[instock] => 110
)
)
)
)

Pushing a value onto the end of an array

I have the following array, it's full of gibberish (I got bored using lorem ipsum so I started typing random stuff -- I'm testing ¬_¬).
I used mysqli_fetch_array to fetch this array.
[array1]
Array
(
[0] => Array
(
[title] => this is a new thread, and it is great and it should work
[thread_id] => 27
[content] => <p>hello, how are you? and what are you doing y'all and this should work</p>
[username] => umar
[author_id] => 12
[tags] => Array
(
[0] => lorem
)
)
[1] => Array
(
[title] => this is my second thread and it should work fine, just fine
[thread_id] => 28
[content] => <p>this is is good, that I think it should have a thread of its own, don't you think?</p>
[username] => umarrazzaq
[author_id] => 12
[tags] => Array
(
[0] => thread
[1] => less
)
)
)
I have another array [array2]:
Array ( [0] => Array ( [replies] => 2 [id] => 27 )
[1] => Array ( [replies] => 1 [id] => 28 ) )
I want to push this second array onto the first array, where the IDs match.
e.g.
So the first array will become:
[0] => Array
(
[title] => this is a new thread, and it is great and it should work
[thread_id] => 27
[content] => <p>hello, how are you? and what are you doing y'all and this should work</p>
[username] => umar
[author_id] => 12
[tags] => Array
(
[0] => lorem
)
**[replies] => 2**
)
I've tried passing by reference and using array_push in a foreach loop, but it only does it for one.
try this:
foreach($array1 as $key=>&$arr){
$arr['replies'] = $array2[$key]['replies']
}
It seems that you could do this also with a JOIN on your table
foreach($array1 as $key => $value)
{
$array1[$value['thread_id']] = $value;
}
foreach($array2 as $value)
{
array_push($array1[$value['id']], $value);
}
if i am getting your question correct then you want to merge array where array2.id and array1.thread_id is same. In that case you can't use array_merge. Unless you change your second array as something like below.
array(
[$thead_id] => Array ( [replies] => 2)
....
)
then you can use array_merge easily .

Categories