0

This it´s my little script, but don´t get right results at the moment :

<?php

    // Delimiters betweeb data "*" elements in each data delimiters ","
    $data_string="house1,403,phone1*house2,404,phone2*house3,403,phone3*house4,405,phone3";
    // Explode $data_string for "~" delimiter
    $data_exp=explode("*",$data_string);
    //

    // Loop 1
    foreach($data_exp as $data_1)
    {
        $data_exp_compar=explode(",",$data_1);

        // We want see the elements with the same data in common in second position (403,404,etc)
        $data_common_1[]=$data_exp_compar[1];
        $data_common_2[]=$data_exp_compar[1];
    }

    $a=array_values(array_intersect_key($data_common_1,$data_common_2));
    $b=array_count_values(array_intersect_key($data_common_1,$data_common_2));

    foreach($a as $aa=>$values)
    {
        echo $aa;
        print "<br>";
    }

?>

The idea in this script. It scans the data inside "$data_string", as you can see, all data delimiters is "*" and inside each data we have elements with "," as delimiter

I want get this output results and in this format :

PRODUCT Id: 403 (2 Actually)

1-  house1,403,phone1
2-  house3,403,phone3 

PRODUCT Id: 404 (1 Actually)

1 - house2,404,phone2

Product Id: 405 (1 Actually)

1 - house4,405,phone4

As you can see the only element for compare it´s in the second position and it´s product´s id

I try many things but i can´t get works, or get finally results as i want show

Thank´s in advanced for all , regards

2 Answers 2

1

You can group them first then another foreach loop for printing result

$data_string="house1,403,phone1*house2,404,phone2*house3,403,phone3*house4,405,phone3";
$data_exp = explode("*",$data_string);

$group = []; // Initialize group array
foreach($data_exp as $data_1)
{
    $data_exp_compar=explode(",",$data_1);
    $group[$data_exp_compar[1]][] = $data_exp_compar; // Group by the number key after exploding
}

// Loop to each group, then print desired format
foreach ($group as $key => $value) {
    echo 'Product ID: ' . $key . ' (' . count($value) . ' Actually)<br>';
    foreach ($value as $k => $v) {
        echo ++$k . ' - ' . implode(',', $v) . '<br>';
    }
    echo '<br>';
}
Sign up to request clarification or add additional context in comments.

Comments

0

I would suggest using array_map and array_filter functions. Let me know if you have questions about this.

<?php

// Prepare data and input
$id = 403;
$data = "house1,403,phone1*house2,404,phone2*house3,403,phone3*house4,405,phone3";

// Convert string data to array
$data = explode("*", $data);
$data = array_map(function ($row) {
    return explode(",", $row);
}, $data);

// Search the array
$response = array_filter($data, function ($row) use ($id) {
    return $row[1] == $id;
});

print_r($response);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.