I'm about using CodeIgniter for my first project. I have tried to get data from database.
$test = $this->komponen_model->get_participants_id()->result_array();
print_r($test)
Array (
[0] => Array ( [id] => 1 )
[1] => Array ( [id] => 4 )
[2] => Array ( [id] => 7 )
)
And I got this. So, I need to call each element by:
foreach ($test as $ts){
echo $ts['id'];
}
My question is, can I make the array shorten. Just like:
Array (
[0] => 1
[1] => 4
[2] => 7
)
I will be appreciated for anyone's advise.
Use array_column
$test = array_column($test,'id');
foreach ($test as $ts){
echo $ts;
}
Related
This question already has answers here:
Convert multidimensional array into single array [duplicate]
(24 answers)
Closed 5 years ago.
It's probably beginner question but I'm going through documentation for
longer time already and I can't find any solution and i have an array which
is multidimensional given below format.
/* This is how my array is currently */
Array
(
[0] => Array
(
[0] => Array
(
[title] => a
[title_num] =>1
[status] => 1
)
[1] => Array
(
[title] => Mr
[title_num] => 82
[status] => 1
)
)
[1] => Array
(
[0] => Array
(
[title] => b
[title_num] =>25
[status] => 2
)
[1] => Array
(
[title] => c
[title_num] =>45
[status] => 2
)
)
)
I want to convert this array into this form
/*Now, I want to simply it down to this*/
Array
(
[0] => Array
(
[title] => a
[title_num] =>1
[status] => 1
)
[1] => Array
(
[title] => Mr
[title_num] => 82
[status] => 1
)
[2] => Array
(
[title] => b
[title_num] =>25
[status] => 2
)
[3] => Array
(
[title] => c
[title_num] =>45
[status] => 2
)
)
I have tried array_flatten,array_map PHP built in function
A link or anything to point me in the right direction will be highly
appreciated
Here is another trick to solve your problem,
function custom_filter($array) {
$temp = [];
array_walk($array, function($item,$key) use (&$temp){
foreach($item as $value)
$temp[] = $value;
});
return $temp;
}
array_walk — Apply a user supplied function to every member of an array
Here is working demo.
here you go
$result = [];
foreach($array as $arr)
{
$result = array_merge($result , $arr);
}
var_dump($result);
Like this:
$i=0;
foreach ($array as $n1) {
foreach ($n1 as $n2) {
$newArr[$i]['title']=$n2['title'];
$newArr[$i]['title_num']=$n2['title_num'];
$newArr[$i]['status']=$n2['status'];
}
$i++;
}
Or simpler as suggested in the comments
for ($i=0; $i < count($array); $i++) {
foreach ($array[$i] as $n2) {
$newArr[$i]['title']=$n2['title'];
$newArr[$i]['title_num']=$n2['title_num'];
$newArr[$i]['status']=$n2['status'];
}
}
I have two array:
$one = Array
(
[0] => stdClass Object
(
[id] => 2
[name] => Southampton
)
[1] => stdClass Object
(
[id] => 4
[name] => Manchester United F.C
)
)
and
$two = Array
(
[0] => stdClass Object
(
[number] => 25555
[slice_1] => 4
[slice_2] => 4
[slice_3] => 2
[slice_4] => 4
[status] => Published
)
)
I want to output, if array $two->slice_1 same as array $one->id than output $one->name.
For example:
$two[0]->slice_1 (4) compare to $one[0]->id (4) will result in Manchester United F.C.
Because the array $one and $two will be have more than one array. Please don't answer this:
if($two[0]->slice_1 == $one[1]->id){echo $one[0]->name;}
I'm stucking here and can't think out some way. Please help. Thanks in advance
avoiding loops inside loops first create temp reference array then loop though second array outputting the result
foreach($one as $id=>$data){
$temp[$data['id']] = $data['name'];
}
foreach($two as $secondary_id=>$secondary_data){
echo isset($temp[$secondary_data['slice_1']]) ? $temp[$secondary_data['slice_1']] : '';
}
Hello Stack Overflow community,
I am trying to update values from array1 with a newly ordered array2 and output the final array as array2. I have been tying to figure this out for days now but cannot seem to get it. I am new to php as you can most likely tell. Can anyone help me?
Here are the two arrays- 'array1' is first and 'array2' follows:
Array (
[0] => Array
(
[id] => bbb
[hammer] => $1,000
)
[1] => Array
(
[id] => ccc
[hammer] => $678
)
[2] => Array
(
[id] => aaa
[hammer] => $222
)
) Array (
[0] => Array
(
[id] => aaa
[hammer] => GBP 135
)
[1] => Array
(
[id] => bbb
[hammer] => GBP 610
)
[2] => Array
(
[id] => ccc
[hammer] => GBP 413
)
)
Now my code tries to update the newly reordered 'id' values in the second array with the 'hammer' values from the first array. Here is my code:
foreach($array2 as $key => $val) {
$a = $array2[$key]['id'];
$hammer_a = $array2[$key]['hammer'];
foreach($array1 as $key => $val) {
$b = $array1[$key]['id'];
if($a===$b){
$hammer_b = $array1[$key]['hammer'];
$array2[$key]['hammer'] = $hammer_b;
}
}
}
However as this code stands I am left with an undesired result below. I would like 'id' 'aaa' to have its original 'hammer' value of $222 and so on for the other 'id's:
Array
(
[0] => Array
(
[id] => aaa
[hammer] => $1,000
)
[1] => Array
(
[id] => bbb
[hammer] => $678
)
[2] => Array
(
[id] => ccc
[hammer] => $222
)
)
Can anyone please tell me what I am doing wrong?
Change the name of $key and $val in the second loop. They are overriding each other.
Don't use $key in your inner foreach loop. You want the key for array2.
There is also this option, with a lot less code:
$items[0] = array('id'=>'bbb','hammer'=>'$1,000');
$items[1] = array('id'=>'ccc','hammer'=>'$678');
$items[2] = array('id'=>'aaa','hammer'=>'$222');
function cmp($a, $b) {
return strcmp($a['id'], $b['id']);
}
echo '<pre>';
var_dump($items);
usort($items, "cmp");
var_dump($items);
echo '</pre>';
I have this array:
SimpleXMLElement Object
(
[id] => Array
(
[0] => Koala.jpg
[1] => Jellyfish.jpg
)
[desc] => Array
(
[0] => koaladesc
[1] => jelly desc
)
[qtidade] => Array
(
[0] => 1
[1] => 5
)
I need create some php function that help me group the values like this:
[0] => Array
(
[0] => Koala.jpg
[1] => koaladesc
[2] => 1
)
[1] => Array
(
[0] => Jellyfish
[1] => jelly desc
[2] => 5
)
Could anyone help me?
Something like this should do the trick, but it's localized to what you're asking based on the vagueness of your question:
$new_array = array();
foreach($simple_xml_object as $obj) {
if(is_array($obj)) {
for($i = 0; $i < count($obj); $i++) {
$new_array[$i][] = $obj[$i];
}
}
}
I would suggest looking at the documentation on the foreach() construct, as well as looking over the SimpleXML manual.
So, you want to tranpose an array. Here's a magical way of transposing rectangular arrays:
array_unshift($array, null);
$array = call_user_func_array('array_map', $array);
let's suppose your array is saved in variable $arrayValues
$arrayValues = [id] => Array
(
[0] => Koala.jpg
[1] => Jellyfish.jpg
)
[desc] => Array
(
[0] => koaladesc
[1] => jelly desc
)
[qtidade] => Array
(
[0] => 1
[1] => 5
)
now you need to create the following code:
foreach($arrayValues as $array)
{
echo $array->id;
echo $array->desc;
echo $array->qtidade;
}
this may work well for you.
I am trying to write some php code to process the second dimension's value of an array based on similar values of the first dimension values.
Following is the sample output.
[0] => Array (
[0] => 1
[1] => 0.091238491238491
)
[1] => Array (
[0] => 2
[1] => 0.2221793635487
)
[2] => Array (
[0] => 2
[1] => 0.10662717512033
)
[3] => Array (
[0] => 4
[1] => 0.44354338998346
)
[4] => Array (
[0] => 6
[1] => 0.2248243559719
)
[5] => Array (
[0] => 6
[1] => 0.31764705882353
)
[6] => Array (
[0] => 6
[1] => 0.15764625384879
)
[7] => Array (
[0] => 6
[1] => 0.19160083160083
)
[8] => Array (
[0] => 12
[1] => 0.31054875069499
)
[9] => Array (
[0] => 12
[1] => 0.10915034227918
)
[10] => Array (
[0] => 15
[1] => 0.32915461266474
)
//...........goes to 46000 elements
Now what I want to do is, if the index 0 values of each array is similar then I want to add the index 1's value.
So for example, if 0 index values for 4 arrays are same , I want to add index 1 values of all 4 arrays.
If there is a unique value on 0th index, dont add it with anything, simply store index 1's value and move on.
Thanks very much.
Ghanshyam
$added = array();
foreach ($array as $item) {
if (isset($added[$item[0]])) {
$added[$item[0]] += $item[1];
} else {
$added[$item[0]] = $item[1];
}
}
$p=0;
$temp = $final_prod_ex[0][1];
for($x=0; $x<count($final_prod)-1; $x++){
if($final_prod_ex[$x][0]==$final_prod_ex[$x+1][0]){
$temp = $temp + $final_prod_ex[$x+1][1];
}
else{
$ans[$p] = $temp." ".$final_prod_ex[$x][0];
$temp = $final_prod_ex[$x+1][1];
$p++;
}
}
Finally figured it out after a lot of thinking(I'm new to programming)...Array's name is $final_prod_ex. Comment on this if I can make it better. And sorry #deceze. I could not understand your solution. I know you were trying to give the value of one array as an index to another. But what the scenario is, that value isnt like 0,1,2,3,4.... Its like 1,3,5,6,7,10. We are missing numbers in between. Maybe I didnt understand your solution. Correct me if I am wrong.
Thanks for all the help.