Grouping distinct values of same index in muldimensional array - php

i got an array like this:
array(28) {
[0]=> array(8) { ["jornada"]=> string(1) "1" ["team1"]=> string(12) "LEGANES F.C." ["team2"]=> string(9) "LAS CONOS" }
[1]=> array(8) { ["jornada"]=> string(1) "1" ["team1"]=> string(13) "BABACHAS F.C." ["team2"]=> string(19) "QUIRQUINCHAS VERDES" }
[2]=> array(8) { ["jornada"]=> string(1) "2" ["team1"]=> string(7) "TSU CHU" ["team2"]=> string(12) "SANTINO F.C." }
[3]=> array(8) { ["jornada"]=> string(1) "2" ["team1"]=> string(11) "LIBERO F.C." ["team2"]=> string(9) "LAS PUMAS" }
[4]=> array(8) { ["jornada"]=> string(1) "3" ["team1"]=> string(6) "PUCARA" ["team2"]=> string(15) "EL REJUNTE F.C." }
my question is, how can i get the differents "jornada's" values? because i have to make a fixture grouping by "jornada" something like :
jornada 1 :
LEGANES vs LAS CONOS
BABACHAS vs QUIRQUINCHAS
jornada 2:
TSU CHU vs SANTINO
LIBERO vs LAS PUMAS
thanks for your help and sorry about my english.

you could re-arrange them into a new array that has this grouping...
$grouped = array();
foreach ($data as $row) {
$grouped[$row['jornada']][] = $row;
}
now you can do:
foreach ($grouped as $jorndada => $matches) {
echo "Jornada $jornada\n";
foreach ($matches as $match) {
echo $match["team1"] ." vs ". $match["team2"] . PHP_EOL;
}
}

Related

Split array with equal indices

I have a result array which is formatted like the following:
array(6) {
[0]=>
array(3) {
["company"]=>
string(1) "A"
["uf_prop"]=>
string(2) "GO"
["qtd_prop_uf"]=>
string(3) "612"
}
[1]=>
array(3) {
["company"]=>
string(1) "A"
["uf_prop"]=>
string(2) "MG"
["qtd_prop_uf"]=>
string(1) "1"
}
[2]=>
array(3) {
["company"]=>
string(1) "A"
["uf_prop"]=>
string(2) "MS"
["qtd_prop_uf"]=>
string(2) "11"
}
[3]=>
array(3) {
["company"]=>
string(1) "A"
["uf_prop"]=>
string(2) "MT"
["qtd_prop_uf"]=>
string(1) "4"
}
[4]=>
array(3) {
["company"]=>
string(1) "A"
["uf_prop"]=>
string(2) "PA"
["qtd_prop_uf"]=>
string(4) "2213"
}
[5]=>
array(3) {
["company"]=>
string(1) "B"
["uf_prop"]=>
string(2) "PA"
["qtd_prop_uf"]=>
string(1) "6"
}
}
I want to split and organize this array like the following example if it is possible:
company: A
uf_prop: GO, MG, MS, MT, PA
qtd_pro_uf: 612, 1, 11, 4, 2213
company: B
uf_prop: PA
qtd_pro_uf: 6
I wish I could divide by 2 or more if there are more companies and then show the states and the amount of a priori stores.
I feel the array structure is like php, so here is php solution.
$result = [];
foreach ($arr as $key => $value) {
$result[$value['company']]['uf_prop'][] = $value['uf_prop'];
$result[$value['company']]['qtd_pro_uf'][] = $value['qtd_pro_uf'];
}
foreach ($result as $key => $value) {
echo "company: " . $key . "<br/>";
echo "uf_prop: " . implode(",", $value['uf_prop']) . "<br/>";
echo "qtd_pro_uf: " . implode(",", $value['qtd_pro_uf']) . "<br/>";
}

how to render a unidimensional and multidimensional array

I am rendering a multidimensional array obtained by a WS, it works perfectly but when changing the structure of the array to multidimensional it shows me an error when rendering. This is my array:
array(5) { ["#attributes"]=> array(1) { ["id"]=> string(13) "08" } ["nom-afi"]=> string(23) "Perez Pedro" ["PRESTAMO"]=> array(2) { [0]=> array(11) { ["num-pr"]=> string(14) "76" ["fec-dsb"]=> string(8) "20180503" ["fec-vnc"]=> string(8) "20230522" ["mto-otor"]=> string(5) "55400" ["plazo"]=> string(20) "60 MESES (60 CUOTAS)" ["tasa"]=> string(1) "0" ["cta-pgadas"]=> string(1) "3" ["cta-mora"]=> string(1) "0" ["sdo-vnc"]=> string(1) "0" ["sdo-actual"]=> string(7) "54382.9" ["cuota-niv"]=> string(7) "1177.09" } [1]=> array(11) { ["num-pr"]=> string(14) "061" ["fec-dsb"]=> string(8) "20180409" ["fec-vnc"]=> string(8) "20230522" ["mto-otor"]=> string(9) "116309.24" ["plazo"]=> string(20) "60 MESES (60 CUOTAS)" ["tasa"]=> string(1) "0" ["cta-pgadas"]=> string(1) "3" ["cta-mora"]=> string(1) "0" ["sdo-vnc"]=> string(1) "0" ["sdo-actual"]=> string(9) "114173.92" ["cuota-niv"]=> string(7) "2471.23" } } }
this is how I render
<?php foreach($PRESTAMOS_LISTADO['PRESTAMO'] as $result): ?>
<option value="<?php echo $result['num-pr']; ?>"><?php echo $result['num-pr']; ?></option>
<?php endforeach; ?>
up to this point it works quite well, until I render this other array:
array(5) { ["#attributes"]=> array(1) { ["id"]=> string(13) "17" } ["nom-afi"]=> string(23) "ALBA" ["PRESTAMO"]=> array(11) { ["num-pr"]=> string(14) "28" ["fec-dsb"]=> string(8) "20180214" ["fec-vnc"]=> string(8) "20190927" ["mto-otor"]=> string(5) "15000" ["plazo"]=> string(20) "18 MESES (18 CUOTAS)" ["tasa"]=> string(1) "0" ["cta-pgadas"]=> string(1) "6" ["cta-mora"]=> string(1) "0" ["sdo-vnc"]=> string(1) "0" ["sdo-actual"]=> string(8) "11058.57" ["cuota-niv"]=> string(6) "900.86" } }
this gave me the next code error:
Warning: Illegal string offset 'num-pr' in C:\xampp\htdocs\proyect\assets\WSHelper.php on line 90
How could I render and apply for multidimensional and unidimensional arrays? Thank you
You could use something like this to check if $PRESTAMOS_LISTADO['PRESTAMO'] is a simple result array and if so, convert it into an array of 1 result:
if (isset($PRESTAMOS_LISTADO['PRESTAMO']['num-pr'])) {
$results = array($PRESTAMOS_LISTADO['PRESTAMO']);
}
else {
$results = $PRESTAMOS_LISTADO['PRESTAMO'];
}
Then you can use $results in your foreach:
<?php foreach ($results as $result): ?>

How to create new arrays from object array based on user_id?

I'm having trouble looping through an object array and displaying data from it under certain conditions.
I want the webpage to display the following:
<p>John Doe</p> <p>William Green</p> <p>Jane Smith</p>
I know how to do this by defining $user[0]->first_name . $user[0]->last_name specifically, but what I need is a way for the code to loop through and display the names dynamically using the user_id property as a unique identifier.
For all objects in the array with user_id 1, return "these values." For all the objects in the array with user_id 2, return "these values." And so on...looping through each unique user_id.
Here is a var_dump of the object array:
array(4)
{ [0]=> object(stdClass)#4336 (4)
{
["umeta_id"]=> string(1) "1"
["user_id"]=> string(1) "1"
["meta_key"]=> string(10) "first_name"
["meta_value"]=> string(4) "John"
}
[1]=> object(stdClass)#4333 (4)
{
["umeta_id"]=> string(1) "2"
["user_id"]=> string(1) "1"
["meta_key"]=> string(9) "last_name"
["meta_value"]=> string(3) "Doe"
}
[2]=> object(stdClass)#4334 (4)
{
["umeta_id"]=> string(1) "3"
["user_id"]=> string(1) "2"
["meta_key"]=> string(10) "first_name"
["meta_value"]=> string(4) "Jane"
}
[3]=> object(stdClass)#4334 (4)
{
["umeta_id"]=> string(1) "4"
["user_id"]=> string(1) "2"
["meta_key"]=> string(9) "last_name"
["meta_value"]=> string(5) "Smith"
}
[4]=> object(stdClass)#4334 (4)
{
["umeta_id"]=> string(1) "5"
["user_id"]=> string(1) "3"
["meta_key"]=> string(10) "first_name"
["meta_value"]=> string(7) "William"
}
[5]=> object(stdClass)#4334 (4)
{
["umeta_id"]=> string(1) "6"
["user_id"]=> string(1) "3"
["meta_key"]=> string(9) "last_name"
["meta_value"]=> string(5) "Green"
}
}
When I output the information on the webpage, I want it to look like this:
<p>John Doe</p> <p>William Green</p> <p>Jane Smith</p>
Maybe I need to loop through objects and build new arrays based on user_ids? Please do not provide code with echo or print_r. Only "return" can be used for this project.
So $your_object is already sorted by umeta_id and user_id fields?
If not, you can use usort:
function cmp1($a, $b)
{
return strcmp($a->umeta_id, $b->umeta_id);
}
function cmp2($a, $b)
{
return strcmp($a->user_id, $b->user_id);
}
usort($your_object, "cmp1");
usort($your_object, "cmp2");
Then simply:
$str = '';
for($i=0;$i<count($your_object);$i+=2){
$str .= "<p>".$your_object[$i]->meta_value." "$your_object[$i+1]->meta_value."</p> ";
}
return $str; // this var contain all your data in string
Hope this helps!
This will generate the first-name and last-name pairs :
$user_group = array();
ksort($user_group, SORT_NUMERIC);
foreach ($users as $key => $item) {
$name_meta = get_object_vars($item);
foreach ($name_meta as $meta_in => $names) {
if ($names == 'first_name') {
$user_group[$item->user_id]['first_name'] = $name_meta['meta_value'];
}
if ($names == 'last_name') {
$user_group[$item->user_id]['last_name'] = $name_meta['meta_value'];
}
}
}
Output :
array(3) {
[1]=>
array(2) {
["first_name"]=>
string(4) "John"
["last_name"]=>
string(3) "Doe"
}
[2]=>
array(2) {
["first_name"]=>
string(4) "Jane"
["last_name"]=>
string(5) "Smith"
}
[3]=>
array(2) {
["first_name"]=>
string(7) "William"
["last_name"]=>
string(5) "Green"
}
}
And to print the user name pairs :
foreach ($user_group as $name) {
echo "<p>" . $name['first_name'] . " " . $name['last_name'] . "</p>";
}

unexpected changes - overriding object inside foreach php

I've got array of object form database like below:
array(3) {
[1]=>
array(2) {
[0]=>
object(stdClass)#99 (3) {
["id"]=>
string(2) "42"
["name"]=>
string(4) "NAME1"
["type"]=>
string(1) "6"
}
[1]=>
object(stdClass)#98 (3) {
["id"]=>
string(3) "146"
["name"]=>
string(3) "STH1"
["type"]=>
string(1) "2"
}
}
[2]=>
array(2) {
[0]=>
object(stdClass)#97 (3) {
["id"]=>
string(2) "422"
["name"]=>
string(4) "NAME2"
["type"]=>
string(1) "3"
}
[1]=>
object(stdClass)#96 (3) {
["id"]=>
string(3) "16"
["name"]=>
string(3) "STH2"
["type"]=>
string(1) "2"
}
}
[3]=>
array(2) {
[0]=>
object(stdClass)#95 (3) {
["id"]=>
string(2) "11"
["name"]=>
string(4) "NAME3"
["type"]=>
string(1) "5"
}
[1]=>
object(stdClass)#94 (3) {
["id"]=>
string(3) "69"
["name"]=>
string(3) "STH3"
["type"]=>
string(1) "3"
}
}
}
And if i want to add the same object to the next array and change value of its type, i override the current object. How can i fix it? My foreach loop below:
foreach($events as $key => $event){
foreach($event as $k => $v){
if($v->type == 6){
$v->type = "0";
$events[$key+1][] = $v;
$v->type = "6";
}
}
}
If my guess what you are trying to achieve is right i would go like this
foreach($events as $key => $event){
foreach($event as $k => $v){
if($v->type == 6){
$tmp = $v;
$tmp->type="0";
$events[$key+1][] = $tmp;
}
}
}

How to print a specific value in array in PHP?

array(2) {
[0]=>
object(stdClass)#144 (7) {
["id"]=>
string(1) "2"
["name"]=>
string(8) "name1"
["value"]=>
string(22) "Lorem Ipsum Dolar Amet"
["type"]=>
string(8) "textarea"
["group"]=>
string(1) "1"
["published"]=>
string(1) "1"
["ordering"]=>
string(1) "1"
}
[1]=>
object(stdClass)#145 (7) {
["id"]=>
string(1) "4"
["name"]=>
string(6) "Link1"
["value"]=>
string(36) "abcabcab"
["type"]=>
string(4) "link"
["group"]=>
string(1) "1"
["published"]=>
string(1) "1"
["ordering"]=>
string(1) "2"
}
}
I want to print only "value" (abcabcab) of id=4. How can I achieve this?
foreach($array as $row){
if($row['id']==4){
print($row['value']);
}
}
foreach ($array as $entry) {
if ($entry['id'] == 4)
echo $entry['value'];
}
array_walk($a, function($el){if($el->id === 4){print $el->value;}});
this works:
foreach ($array as $entry) {
if ($entry->id == 4)
echo $entry->value;
}
Thanks!

Categories