I have an array like this
array(5) {
[0]=> array(2) { [0]=> string(5) "REFER" [1]=> string(12) "Não Sócios" }
[1]=> array(2) { [0]=> string(5) "REFER" [1]=> string(12) "Não Sócios" }
[2]=> array(2) { [0]=> string(5) "REFER" [1]=> string(12) "Não Sócios" }
[3]=> array(2) { [0]=> string(5) "REFER" [1]=> string(12) "Não Sócios" }
[4]=> array(2) { [0]=> string(5) "REFER" [1]=> string(12) "Não Sócios" }
}
and want to transform this array into an array like this
array("REFER, Não Sócios", "REFER, Não Sócios", "REFER, Não Sócios", "REFER, Não Sócios", "REFER, Não Sócios");
is that possible? if so, how can i do it?
thanks in advance
Yes. Assuming that array is called $refer -
$new_refer = array_map(function($val) {
return implode(', ', $val);
}, $refer);
Using parray_map you iterate over the elements and construct a new array with values returned from the callback function. Inside that function i'm using implode to combine the values of each element (which is an array in itself) and glue them with ', '.
You can do it with array_map for example:
$r = array_map(function($v) { return $v[0] . ', ' . $v[1]; }, $source_array);
here's a way you can do it:
$a = [
[ 0 => "REFER", 1 => "Nao Socios" ] ,
[ 0 => "REFER", 1 => "Nao Socios" ] ,
[ 0 => "REFER", 1 => "Nao Socios" ] ,
[ 0 => "REFER", 1 => "Nao Socios" ] ,
[ 0 => "REFER", 1 => "Nao Socios" ]
];
$res = [];
foreach ($a as $k => $v) {
$res[] = $v[0];
$res[] = $v[1];
}
var_dump($res);
Related
I need to convert simple array to nested array according to specific rules. I've achived it but I'm looking for better solution.
SIMPLE:
array(4) {
[0]=>
array(2) {
["id"]=>
string(2) "11"
["type"]=>
int(3)
}
[1]=>
array(2) {
["id"]=>
string(2) "10"
["type"]=>
int(2)
}
[2]=>
array(2) {
["id"]=>
string(1) "1"
["type"]=>
int(1)
}
[3]=>
array(2) {
["id"]=>
string(1) "0"
["type"]=>
int(1)
}
}
EXPECTED EFFECT:
array(1) {
[0]=>
array(2) {
["type"]=>
int(1)
["child"]=>
array(1) {
[1]=>
array(2) {
["type"]=>
int(1)
["child"]=>
array(1) {
[10]=>
array(2) {
["type"]=>
int(2)
["child"]=>
array(1) {
[11]=>
array(2) {
["type"]=>
int(3)
["child"]=>
array(0) {
}
}
}
}
}
}
}
}
}
MY SOLUTION (not very satisfying):
$nestedArray = [];
foreach ($simpleArray as $item)
{
if (!empty($nestedArray))
{
$array = $nestedArray;
reset($array);
$firstKey = key($array);
}
$nestedArray[$item['id']]['child'] = $nestedArray;
$nestedArray[$item['id']]['type'] = $item['type'];
if (!empty($firstKey))
{
unset($nestedArray[$firstKey]);
}
}
As I said, I'm looking for more elegant way to achieve that. Rule are very simply: every next item is child of previous.
You could use recursion:
function nest($arr) {
return count($arr) ? ["type" => array_pop($arr)["type"], "child" => nest($arr)] : [];
}
With your example input, it would look like this:
$simpleArray = [
["id" => "11", "type" => 3],
["id" => "10", "type" => 2],
["id" => "1", "type" => 1],
["id" => "0", "type" => 1]
];
function nest($arr) {
return count($arr) ? ["type" => array_pop($arr)["type"], "child" => nest($arr)] : [];
}
$nested = nest($simpleArray));
$nested will have the following value:
[
"type" => 1,
"child" => [
"type" => 1,
"child" => [
"type" => 2,
"child" => [
"type" => 3,
"child" => []
]
]
]
]
This is how my array looks like:
array(3) {
[0]=>
string(3) "600"
[1]=>
string(3) "601"
[2]=>
string(3) "603"
}
This is how my object looks like:
array(7) {
[0]=>
object(stdClass)#688 (6) {
["id"]=>
string(3) "601"
["name"]=>
string(10) "test8opkpo"
["avatar"]=>
string(85) "http://avatars/user/medium.png"
["url"]=>
string(86) "/index.php"
["isOnline"]=>
int(0)
["lastseen"]=>
string(11) "2 weeks ago"
}
[1]=>
object(stdClass)#689 (6) {
["id"]=>
string(3) "604"
["name"]=>
string(6) "nopita"
["avatar"]=>
string(85) "http://avatars/user/medium.png"
["url"]=>
string(82) "/index.php"
["isOnline"]=>
int(0)
["lastseen"]=>
string(10) "1 week ago"
}
[2]=>
object(stdClass)#690 (6) {
["id"]=>
string(3) "603"
["name"]=>
string(6) "test_b"
["avatar"]=>
string(85) "http://avatars/user/medium.png"
["url"]=>
string(82) "/index.php"
["isOnline"]=>
int(0)
["lastseen"]=>
string(11) "6 hours ago"
}
Now I want to remove from the object, each item's id that matches the value inside the array.
So final output of the object should not contain id's that present in the array given. How to do that?
I tried using array_diff_key and unset to no avail.
$contactArray[$i] represent each id in the object
if (in_array($contactArray[$i], $array)) {
$a = array_diff_key($results->contacts, [$i => $contactArray[$i]]);
}
I created my own set of examples to simulate what you want to happen on your array:
$x = array('600','601', '603');
$y = array(
array("id" => "600",
"name" => "test",
"avatar" => "image"
),
array("id" => "601",
"name" => "test1",
"avatar" => "image1"
),
array("id" => "602",
"name" => "test2",
"avatar" => "image2"
),
array("id" => "603",
"name" => "test3",
"avatar" => "image3"
),
array("id" => "604",
"name" => "test4",
"avatar" => "image4"
)
);
echo '<pre>';
var_dump($y);
echo '</pre>';
$new_arr_ = array();
for($i = 0, $ctr = count($y); $i < $ctr; $i++) {
if(!in_array($y[$i]["id"], $x)) {
$new_arr_[] = array($y[$i]["id"], $y[$i]["name"], $y[$i]["avatar"]);
}
}
echo '<pre>';
var_dump($new_arr_);
echo '</pre>';
Hope it helps.
If I understand you correctly the following should work:
$contactArray = array_filter($contactArray, function ($v) use ($array) {
return !in_array(isset($v->id)?$v->id:null, $array);
});
This question already has answers here:
How to GROUP BY and SUM PHP Array? [duplicate]
(2 answers)
Closed 5 months ago.
given this array:
array(40) {
[0]=>
array(10) {
["item"]=>
string(5) "AABBCC"
["quants"]=>
string(1) "1"
}
[1]=>
array(10) {
["item"]=>
string(5) "AABBCC"
["quants"]=>
string(1) "1"
}
[2]=>
array(10) {
["item"]=>
string(5) "SLF02"
["quants"]=>
string(1) "1"
}
[3]=>
array(10) {
["item"]=>
string(5) "SLF02"
["quants"]=>
string(1) "3"
}
}
how without using a foreach do I end up with this output:
array(40) {
[0]=>
array(10) {
["item"]=>
string(5) "AABBCC"
["quants"]=>
string(1) "2"
}
[1]=>
array(10) {
["item"]=>
string(5) "SLF02"
["quants"]=>
string(1) "3"
}
}
are there any array_sum functions to do this with a multidimensional array like this in php?
This is a bad idea, but seemed like a fun challenge to do without a foreach:
$arr =
[
[
"item" =>"AABBCC",
"quants" => "1",
],
[
"item" => "AABBCC",
"quants" => "1",
],
[
"item" => "SLF02",
"quants" => "1",
],
[
"item" => "SLF02",
"quants" => "3",
]
];
$arr = array_values(call_user_func_array("array_merge", array_map(function($i) use ($arr) {
return [$i["item"] => ["item" => $i["item"], "quants" => array_reduce(
array_filter($arr, function($j) use ($i) {
return $j["item"] == $i["item"];
}), function($carry, $item) {
return $carry + $item["quants"];
})
]];
}, $arr)));
var_dump($arr);
/*
array(2) {
[0]=>
array(2) {
["item"]=>
string(6) "AABBCC"
["quants"]=>
int(2)
}
[1]=>
array(2) {
["item"]=>
string(5) "SLF02"
["quants"]=>
int(4)
}
}
*/
Here's my approach:
<?php
$array = array(
array('item'=>'AABBCC','quants'=>1),
array('item'=>'AABBCC','quants'=>1),
array('item'=>'SLF02','quants'=>1),
array('item'=>'SLF02','quants'=>3),
);
$summed_array = array();
foreach($array as $row){
$key = $row['item'];
if(!isset($summed_array[$key])){
$summed_array[$key] = array(
'item' => $row['item'],
'quants' => 0
);
}
$summed_array[$key]['quants'] += $row['quants'];
}
// turn the array back to a 0 based array
$summed_array = array_values($summed_array);
echo '<pre>',print_r($summed_array),'</pre>';
The question might be silly. But I am stucked in here. :( I am getting all of my database value as an object in controller.
This is how I am fetching database value:
$points = $this->CI->modelgraph->get($user_id);
It is getting all of the data corresponding to that user. This is my sample database table from where I am fetching data:
id user_id b_value h_value date r_value
1 24 330 6.2 10.11.2014 90
2 25 334 6.2 10.11.2014 92
This is static data, phpgraphlib provide in the tutorial.
$data = array("1" => .0032, "2" => .0028, "3" => .0021, "4" => .0033,
"5" => .0034, "6" => .0031, "7" => .0036, "8" => .0027, "9" => .0024,
"10" => .0021, "11" => .0026, "12" => .0024, "13" => .0036,
"14" => .0028, "15" => .0025);
I need to extract the fetched data in this manner:
$data = array("h_value" => b_value,);
How could achieve this? What will be the logic? Any help would be really appreciable.
Here is my approach.. but also not complete.. What I am doing wrong?
$total=count($points);
$i=1;
$dataArray=array();
while($i <= $total) {
foreach($points as $value) {
//echo $value -> h_value;
$field = $value -> h_value;
$labels = $value -> b_value;
$dataArray[$i][$field ] = $labels;
$i++;
}
}
When I var_dump($dataArray); Its giving me this output:
array(11) { [1]=> array(1) { ["6.6"]=> string(3) "358" } [2]=> array(1) { ["7.4"]=> string(3) "201" } [3]=> array(1) { ["6.5"]=> string(3) "144" } [4]=> array(1) { ["6.5"]=> string(3) "112" } [5]=> array(1) { ["6.2"]=> string(3) "144" } [6]=> array(1) { ["6.2"]=> string(3) "185" } [7]=> array(1) { ["7.0"]=> string(3) "176" } [8]=> array(1) { ["7.5"]=> string(3) "234" } [9]=> array(1) { ["6.5"]=> string(3) "365" } [10]=> array(1) { ["6.2"]=> string(3) "110" } [11]=> array(1) { ["4.2"]=> string(3) "100" } }
But When I var_dump($data); Its giving this:
array(15) { [1]=> float(0.0032) [2]=> float(0.0028) [3]=> float(0.0021) [4]=> float(0.0033) [5]=> float(0.0034) [6]=> float(0.0031) [7]=> float(0.0036) [8]=> float(0.0027) [9]=> float(0.0024) [10]=> float(0.0021) [11]=> float(0.0026) [12]=> float(0.0024) [13]=> float(0.0036) [14]=> float(0.0028) [15]=> float(0.0025) }
Its clearly visible I have messed up . Where is the wrong ?
Here is the output I print_r($points); Its giving this:
Array ( [0] => stdClass Object ( [id] => 12 [user_id] => 24 [b_value] => 358 [h_value] => 6.6 [rec_date] => 2014-09-19 [rec_time] => [h_value] => 1[date_added] => 2012-09-19 16:38:05 [date_modified] => 0000-00-00 00:00:00 ) [1] ..........
Please see below code.
It is self-explanatory.
Replace your values accordingly.
<?php
function modelFunction($user_id) {
$this->db->select('h_value, b_value');
$this->db->from('YOUR_TABLE_NAME');
$this->db->where('user_id', $user_id);
$query = $this->db->get();
$arr = array();
if ($query->num_rows()) {
$i=0;
foreach ($query->result_array() as $row) {
extract($row);
$arr[$i][$h_value] = $b_value;
++$i;
}
}
}
?>
Controller:
<?php
$points = $this->CI->modelFunction->get($user_id);
?>
Answer edited as per OP's request:
<?php
$dataArray = array();
for ($i=0 ; $i<= $total ; $i++) {
$value = $points[$i];
$field = $value->h_value;
$labels = $value->b_value;
$dataArray[$i][$field ] = $labels;
$i++;
}
?>
$new_array = array();
foreach($points as $point){
$new_array[] = array(
'id' => $point->id,
'user_id' => $point->user_id
);
}
Nice little helper function:
function object_to_array($data)
{
if (is_object($data)) {
$data = get_object_vars($data);
}
if (is_array($data)) {
return array_map(__FUNCTION__, $data);
}
else {
return $data;
}
}
example usage:
$data['points'] = $this->CI->modelgraph->get($user_id);
$data['points'] = object_to_array($data['points']);
I have an array like:
print_r($arr);
array(2) {
["'type'"]=>
array(3) {
[0]=>
string(17) "tell" // <----
[1]=>
string(6) "mobile" // <----
[2]=>
string(6) "address" // <----
}
["'value'"]=>
array(3) {
[0]=>
string(11) "+00.0000000" // tell
[1]=>
string(11) "12345678" // mobile
[2]=>
string(11) "Blah SQ." // address
}
}
I want a final string like:
tell = +00.0000000<br />mobile = 12345678<br />address = Blah SQ.
Now it's been more than an hour I'm struggling with this but no results yet, anyone could help me with this? I would appreciate anykind of help.
Thanks
=======================================
What I have tried:
$arr is an array so I did:
foreach($arr as $values){
// here also $values is an array, so I needed another foreach to access to items:
$i = 0;
foreach($values as $items){
// now making the final output
#$output.= $items['type'][$i] . '=' . $items['value'][$i] . '<br />';
$i++;
}
}
I'd go for array_combine(). Basically it does what you request:
$yourArray = [
"type" => ["tell", "mobile", "address"],
"value" => ["+00.0000000", "12345678", "Blah SQ."]
];
$combined = array_combine($yourArray["type"], $yourArray["value"]);
will be
$combined = [
"tell" =>"+00.0000000",
"mobile" =>"12345678",
"address" =>"Blah SQ."
];
Lastly, you can iterate through that array and then join the values:
$finalArray=array();
foreach($combined as $type=>$value)
$finalArray[]="$type=$value";
$string = join("<br/>", $finalArray); // Will output tell=+00.000000<br/>mobile=12345678<br/>address=Blah SQ.
It's not the fastest method but you'll learn quite a bit about arrays.
EDIT (using array_combine by #Dencker)
foreach($arr as $values) {
// here also $values is an array, so I needed another foreach to access to items:
$v = array_combine($values["'type'"], $values["'value'"]);
foreach($v as $key => $val) {
// now making the final output
$output.= $key . '=' . $val . '<br />';
}
}
Try this
$arr = array(
array(
"'type'" => array('tell', 'mobile', 'address'),
"'value'" => array('+00000', '123123', 'foo')
),
array(
"'type'" => array('tell', 'mobile', 'address'),
"'value'" => array('+10000', '123123', 'bar')
),
array(
"'type'" => array('tell', 'mobile', 'address'),
"'value'" => array('+20000', '123123', 'foobar')
),
);
var_dump($arr);
$output = '';
foreach($arr as $values) {
// here also $values is an array, so I needed another foreach to access to items:
$i = 0;
foreach($values as $items) {
// now making the final output
$output.= $values["'type'"][$i] . '=' . $values["'value'"][$i] . '<br />';
$i++;
}
}
echo $output;
You were referencing the other array in the second loop.
=============================================================
EDIT:
var_dump($arr);
array(3) {
[0]=> array(2) {
["type"]=> array(3) {
[0]=> string(4) "tell"
[1]=> string(6) "mobile"
[2]=> string(7) "address"
}
["value"]=> array(3) {
[0]=> string(6) "+00000"
[1]=> string(6) "123123"
[2]=> string(3) "foo"
}
}
[1]=> array(2) {
["type"]=> array(3) {
[0]=> string(4) "tell"
[1]=> string(6) "mobile"
[2]=> string(7) "address"
}
["value"]=> array(3) {
[0]=> string(6) "+10000"
[1]=> string(6) "123123"
[2]=> string(3) "bar"
}
}
[2]=> array(2) {
["type"]=> array(3) {
[0]=> string(4) "tell"
[1]=> string(6) "mobile"
[2]=> string(7) "address"
}
["value"]=> array(3) {
[0]=> string(6) "+20000"
[1]=> string(6) "123123"
[2]=> string(6) "foobar"
}
}
}
OUTPUT:
tell=+00000
mobile=123123
tell=+10000
mobile=123123
tell=+20000
mobile=123123