Update all values of PHP array having same index - php

This is my array structure:
Array
(
[0] => Array
(
[0] => Array
(
[topic_id] =>
[user_id] => ZGNjBQN9ac3K
[owner_id] => 15157
[tagged_field] => description
[created_date] => 2015-02-06 12:11:54
)
[1] => Array
(
[topic_id] =>
[user_id] => ZGNjAmD9ac3K
[owner_id] => 15157
[tagged_field] => description
[created_date] => 2015-02-06 12:11:54
)
)
)
I generate this structure before saving topics so i will get topic id only after this.
So in-order to save this array i need to set topic id to all index "topic_id"... lets say if topic_id is 11234 i need to updated all index with topic_id with value 11234.
Desired Output:
Array
(
[0] => Array
(
[0] => Array
(
[topic_id] => 11234
[user_id] => ZGNjBQN9ac3K
[owner_id] => 15157
[tagged_field] => description
[created_date] => 2015-02-06 12:11:54
)
[1] => Array
(
[topic_id] => 11234
[user_id] => ZGNjAmD9ac3K
[owner_id] => 15157
[tagged_field] => description
[created_date] => 2015-02-06 12:11:54
)
)
)

Try with -
$indexedArray = array();
foreach($yourArray as $value) {
foreach($value as $val) {
$indexedArray[$val['topic_id']][] = $val;
}
}

You need to use references in both foreach loops:
$arr = array(
array(
array("topic_id" => "", "user_id" => "ZGNjBQN9ac3K", "owner_id" => "15157", "tagged_field" => "description", "created_date" => "2015-02-06 12:11:54"),
array("topic_id" => "", "user_id" => "ZGNjBQN9ac3K", "owner_id" => "15157", "tagged_field" => "description", "created_date" => "2015-02-06 12:11:54"),
)
);
foreach($arr as &$value) {
foreach($value as &$val) {
$val['topic_id'] = $newvalue;
}
}
print_r($arr);

You can do by simple way using reference variable like
$yourArray = array(array(array("topic_id"=>"","user_id"=>110),array("topic_id"=>"","user_id"=>786)));
foreach($yourArray as &$value) {
foreach($value as &$val) {
$val['topic_id'] = "your_topic_id";
}
}
Output
Array
(
[0] => Array
(
[0] => Array
(
[topic_id] => your_topic_id
[user_id] => 110
)
[1] => Array
(
[topic_id] => your_topic_id
[user_id] => 786
)
)
)

Related

make inline result from array value

I have two array as bellow :
First array :
Array
(
[0] => Array
(
[0] => Array
(
[name] => one
[number] => 051
)
[1] => Array
(
[name] => two
[number] => 052
)
[2] => Array
(
[name] => three
[number] => 053
)
)
[1] => Array
(
[0] => Array
(
[name] => four
[number] => 061
)
[1] => Array
(
[name] => five
[number] => 062
)
)
)
I want to make output from first array above
[0] => 051, 052, 053.
[1] => 061, 062.
Array
(
[0] => Array
(
[0] => Array
(
[name] => book
[number] => 41
)
[1] => Array
(
[name] => pencil
[number] => 42
)
)
[1] => Array
(
[name] => eraser
[number] => 71
)
)
I want to make output from second array above
[0] => 41, 42.
[1] => 71.
Please advise. Thank you.
You can make a try like this way with two foreach() loop.
$numbers = [];
foreach ($array as $k => $v) {
$num = [];
foreach ($v as $k2 => $v2) {
$num[] = $v2['number'];
}
$numbers[$k] = implode(',',$num).'.';
}
print_r($numbers);
DEMO: https://3v4l.org/mEeO7
you can try something like this
$arr = Array (
Array (
Array (
"name" => "one",
"number" => "051"
),
Array (
"name" => "two",
"number" => "052"
),
Array (
"name" => "three",
"number" => "053"
)
),
Array (
Array (
"name" => "four",
"number" => "061"
),
Array (
"name" => "five",
"number" => "062"
)
)
);
foreach ($arr as $k => $s_arr) {
echo "[" . $k . "] => ";
foreach ($s_arr as $k2 => $v2) {
echo $v2["number"] . " ";
}
echo "\n";
}

Change key of multidimensional array

I have following array as response from db. I am trying to convert this database response into multidimensional array as per my requirement.
Array
(
[0] => Array
(
[0] => Array
(
[_id] => C10359
[AE] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
)
[1] => Array
(
[_id] => C10428
[AE] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
)
[1] => Array
(
[0] => Array
(
[_id] => C10350
[AE] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
)
[1] => Array
(
[_id] => C10430
[AE] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
)
)
Now I need to convert above array in following way.
Array
(
[0] => Array
(
[C10359] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
[C10428] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
[1] => Array
(
[C10350] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
[C10430] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
)
following is way i am trying
array_map(function($arr) {
return $arr[0] ;
},$panel_result);
But it is not working.
Kindly suggest how can I convert in required formate.
This should do the trick :
$arr = array(
array(
array(
'_id' => 'C10359',
'AE' => array
(
89785,
89786,
89857,
89859,
),
),
array(
'_id' => 'C10428',
'AE' => array
(
50191,
50203,
50230,
50244,
),
),
),
);
$output = array();
foreach ($arr as $levelK => $level) {
if(!isset($output[$levelK])){
$output[$levelK] = array();
}
foreach ($level as $subLevel) {
$id = $subLevel['_id'];
if (!isset($output[$levelK][$id])) {
$output[$levelK][$id] = array();
}
foreach ($subLevel['AE'] as $val) {
$output[$levelK][$id][] = $val;
}
}
}
Hope this helps.
Use array_column() and pass third param as the index key.
$reqArray = array();
foreach ($yourArray as $key => $innerArray) {
$reqArray[] = array_column($innerArray, 'AE', '_id');
}
OR
Use array map()
$reqArray = array_map(function($a){
return array_column($a, 'AE', '_id');
},$arr);

PHP Array re-arrange to Multi Dimensional

I have an array structure like this and wanted to Re-arrange it to the one below. Any suggestions for a faster/simple fix? I already did the addition of the dates. Thanks! :)
Input:
Array
(
[0] => Array
(
[user_id] => 255
[display_name] => Mark
[company_name] => Company_A
)
[1] => Array
(
[user_id] => 150
[display_name] => Paul
[company_name] => Company_A
)
[2] => Array
(
[user_id] => 25
[display_name] => Hulk
[company_name] => Company_B
)
[3] => Array
(
[user_id] => 50
[display_name] => Bob
[company_name] => Company_B
)
)
Output:
Array
(
[Company_A] => Array
(
[company_total_hours] => 20h 45m
[employees] => Array
(
[0] => Array
(
[user_id] => 255
[display_name] => Mark
)
[1] => Array
(
[user_id] => 150
[display_name] => Paul
)
)
)
[Company_B] => Array
(
[company_total_hours] => 7h 30m
[employees] => Array
(
[0] => Array
(
[user_id] => 25
[display_name] => Hulk
)
[1] => Array
(
[user_id] => 50
[display_name] => Bob
)
)
)
)
My Attempts:
<?php
$company_names = array();
foreach ($records as $k => $v) {
$company_names[] = $v->company_name;
}
$company_names = array_unique($company_names);
// hard coded testing
if (count($company_names) > 0) {
foreach($company_names as $k2 => $v2) {
$final_array[$v2]['company_total_hours'] = rand(1, 20);
$final_array[$v2]['employees'] = array(
array('user_id' => '255', 'display_name' => 'Mark'),
array('user_id' => '150', 'display_name' => 'Paul')
);
}
}
// on-going testing right now here....
I don't see where you derive your hours from so I left that out.
$i = 0;
foreach($vals as $keys => $arrays) {
if(!isset($new[$arrays['company_name']]))
$i = 0;
$new[$arrays['company_name']]['employees'][$i]['display_name'] = $arrays['display_name'];
$new[$arrays['company_name']]['employees'][$i]['user_id'] = $arrays['user_id'];
$i++;
}
Gives you:
Array
(
[Company_A] => Array
(
[employees] => Array
(
[0] => Array
(
[display_name] => Mark
[user_id] => 255
)
[1] => Array
(
[display_name] => Paul
[user_id] => 150
)
)
)
[Company_B] => Array
(
[employees] => Array
(
[0] => Array
(
[display_name] => Hulk
[user_id] => 25
)
[1] => Array
(
[display_name] => Bob
[user_id] => 50
)
)
)
)
foreach($arr as $v)
{
if(!$arr2[$v['company_name']]['employees'])
$arr2[$v['company_name']]['employees'] = array();
if(!$arr2[$v['company_name']]['company_total_hours'])
$arr2[$v['company_name']]['company_total_hours'] = '2h';//addional value
$arr2[$v['company_name']]['employees'][] = array('user_id'=>$v['user_id'],
'display_name'=>$v['display_name']
);
}
I have created a function which does the same thing as the answer given by #Rasclatt.
function groupByKeyValue($array, $oldKeyName, $newKeyName){
$newArray = array();
foreach($array as $key=>$value){
if(isset($newArray[$value[$oldKeyName]][$newKeyName])){
$newArray[$value[$oldKeyName]][$newKeyName][] = array('user_id'=> $value['user_id'], 'display_name' => $value['display_name']);
}else{
$newArray[$value[$oldKeyName]] = array($newKeyName => array(array('user_id'=> $value['user_id'], 'display_name' => $value['display_name'])));
}
}
return $newArray;
}
//usage
$newArray = groupByKeyValue($array, 'company_name', 'employees');
You can add a third parameter to send the keys for the array values which needs to be used in the new array for 'employees'. Please check this link for the working of the function http://goo.gl/I6Of5y

How to merge array index if the value is same of another index?

I just pasted my sample input & output.
Sample Input:
Array
(
[0] => Array
(
[id] => 1
[msisdn] => 10
[sc] => 8155
)
[1] => Array
(
[id] => 2
[msisdn] => 20
[sc] => 22020
)
[2] => Array
(
[id] => 3
[msisdn] => 10
[sc] => 8155
)
[3] => Array
(
[id] => 4
[msisdn] => 10
[sc] => 8155
)
[4] => Array
(
[id] => 5
[msisdn] => 20
[sc] => 22020
)
[5] => Array
(
[id] => 6
[msisdn] => 30
[sc] => 22020
)
)
Sample Output:
Array
(
[0] => Array
(
[id] => 1,3,4
[msisdn] => 10
[sc] => 8155
)
[1] => Array
(
[id] => 2,5
[msisdn] => 20
[sc] => 22020
)
[2] => Array
(
[id] => 6
[msisdn] => 30
[sc] => 8155
)
)
Just make that particular value that key, then just concatenate when already pushed/exists:
$new_array = array();
foreach ($array as $value) {
if(!isset($new_array[$value['msisdn']])) {
// if not yet pushed, just initialize
$new_array[$value['msisdn']] = $value;
} else {
// if already inside, then just concatenate
$new_array[$value['msisdn']]['id'] .= ', ' . $value['id'];
}
}
$new_array = array_values($new_array);
echo '<pre>';
print_r($new_array);
Sample Output
Live on codepad: http://codepad.org/0fw9k2w9
You can use the fact that in PHP array is an hash map. By creating an intermediate array you can solve this in O(n) time.
$merged = array();
foreach($array as $v) {
$merged[$v['msisdn']][$v['sc']] [] = $v['id'];
}
$final = array();
foreach($merged as $msisdn=>$v) {
foreach($v as $sc=>$ids) {
$final [] = array('msisdn'=>$msisdn,'sc'=>$sc,'id'=>$ids);
}
}

Extract a value from a data structure to a variable

I need help in extracting "Duration" from the data structure below, to a variable called var_dur.
The data comes from: print_r($data);
Guzzle\Service\Resource\Model Object
(
[structure:protected] =>
[data:protected] => Array
(
[Job] => Array
(
[Arn] => arn:aws:elastictranscoder:us-west-2:98yufdos8u:job/fsdoiufds98u
[Id] => fdsu98sdufio
[Input] => Array
(
[AspectRatio] => auto
[Container] => auto
[FrameRate] => auto
[Interlaced] => auto
[Key] => iudyf98udsf
[Resolution] => auto
)
[Output] => Array
(
[AlbumArt] =>
[Composition] =>
[Duration] => 31
[Height] => 522
[Id] => 1
[Key] => dlsjf9ds8uf9d8sjuf9s.mp4
[PresetId] => sdufhy89dsfu98dsf
[Rotate] => 0
[SegmentDuration] =>
[Status] => Complete
[StatusDetail] =>
[ThumbnailPattern] => filename-700thumb-{resolution}-{count}
[Watermarks] => Array
(
)
[Width] => 640
)
[OutputKeyPrefix] =>
[Outputs] => Array
(
[0] => Array
(
[AlbumArt] =>
[Composition] =>
[Duration] => 31
[Height] => 522
[Id] => 1
[Key] => dlsjf9ds8uf9d8sjuf9s.mp4
[PresetId] => duisfy98dsuf89sd
[Rotate] => 0
[SegmentDuration] =>
[Status] => Complete
[StatusDetail] =>
[ThumbnailPattern] => filename-700thumb-{resolution}-{count}
[Watermarks] => Array
(
)
[Width] => 640
)
)
[PipelineId] => dsuf89dsuf89d
[Playlists] => Array
(
)
[Status] => Complete
)
)
)
Do something like this to iterate over the Array
function searchfor($text, $array) {
foreach (array_expression as $key => $val)
if($key == $text){
return $val
}
if(is_array($var){
return searchfor($text, $var);
}
}
return null;
}
Try it
$reflect = new ReflectionClass(OBJECT);
$props = $reflect->getProperties();
foreach ($props as $prop) {
print $prop->getName();
var_dump($prop->getValue());
}
About reflection class http://www.php.net/manual/en/reflectionclass.getproperties.php
About reflection property http://www.php.net/manual/en/class.reflectionproperty.php

Categories