0

Having trouble wrapping my head around this conceptually. Still new to this. Basically I have this return from my database :

array (size=456)
  0 => 
    object(stdClass)[358]
      public 'id' => string '2432' (length=4)
      public 'symbol' => string '.AMLP' (length=14)
      public 'last' => string '0.01' (length=4)
      public 'volume' => string '3690' (length=4)
      public 'the_date' => string '2019-09-13' (length=10)
      public 'the_screener' => string '1' (length=1)
      public 'notes' => string 'notes here' (length=149)
  1 => 
    object(stdClass)[726]
      public 'id' => string '2417' (length=4)
      public 'symbol' => string '.ARCC' (length=14)
      public 'last' => string '2.25' (length=4)
      public 'volume' => string '1633' (length=4)
      public 'the_date' => string '2019-09-13' (length=10)
      public 'the_screener' => string '1' (length=1)
      public 'notes' => string 'notes' (length=60)
  2 => 
    object(stdClass)[726]
      public 'id' => string '2447' (length=4)
      public 'symbol' => string '.ARCC' (length=14)
      public 'last' => string '2.25' (length=4)
      public 'volume' => string '1633' (length=4)
      public 'the_date' => string '2019-09-12' (length=10)
      public 'the_screener' => string '1' (length=1)
      public 'notes' => string 'notes here 3' (length=60)
  3 => 

What I'm trying to do with PHP is create an object/array that I can work with that displays these items like

AMLP 1 found on dates 2019-09-13
ARCC 2 found on dates 2019-09-13, 2019-09-12

In the end I would display these in a table, but conceptually this is what I'm trying to do.

I've tried creating an array in my foreach I use to display this information in a table, but I was thinking about it and it's probably better to just use the same query data and break it down separately.

So I'd like to create an array like :

    Array
(
    [1] => Array
        (
            [id] => 1
            [symbol] => ARCC 
            [dates] => Array
                (
                    [3] => Array
                        (
                            2019-09-13
                            2019-09-12
                        )

                )

        )

)
1
  • Possible duplicate of php array group Commented Sep 14, 2019 at 5:35

1 Answer 1

2

Consider your array is $arrDates. If you want to access properties like object properties

$arrFinal = [];
foreach ($arrDates as $intKey => $obj){
    $strSymbol = getSubSymbol($obj->symbol);
    if(!isset($arrFinal[$strSymbol])) {
        $arrFinal[$strSymbol] = [ 'id' => $obj->id, 'symbol' => $obj->symbol];
    }

    $arrFinal[$strSymbol]['dates'][] = $obj->the_date;
 }

// Now loop throuh arrFinal and do print the statements you want.
foreach($arrFinal as $strSubSymbol => $arrData){
    echo $strSubSymbol . ' '. count($arrData['dates']) . ' found on dates ' . implode(',', $arrData['dates']). PHP_EOL;
}

Function to get the desired subpart of symbol

function getSubSymbol($symbol_original){
    $symbol = preg_split('/(?=\d)/', $symbol_original, 2); //get everything up until first number or the date in the string in this case.
    $symbol_here = substr($symbol[0], 1);
    return $symbol_here;
}
Sign up to request clarification or add additional context in comments.

7 Comments

Hi Jitendra . I haven't been able to full go through but right now this is the return : Array ( [] => Array ( [id] => [symbol] => [dates] => Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => The query results come into $products also.
I hope so as you have object inside, try the second one in which I've mentioned that
I think I forgot an important part, but I think we're on the right track here. Thank you for your help with this by the way. The symbol is returned like this : AMLP191101C10 or like this ARCC210115C17 Notice that the symbol is the first letters before the first number. I think I need to add that in some way for this to work, right? It's spitting out the array, but no matches.
Do you mean, here we are comparing AMLP but it can be AMLP191101C10 or AMLP1913433C39, is that so?
Yes, just the first letters before the first number in the string. Yes your extrapolation is correct above. The output is still not showing any matches as before. I think because the symbol from what I'm reading in the script. Can you explain the index above. Maybe I'm just not understanding what you mean by that.
|

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.