Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8929973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:47:05+00:00 2026-06-15T08:47:05+00:00

How can i create a new array from elements in associative array in the

  • 0

How can i create a new array from elements in associative array in the way that if values is integer then put that value on first place in a new array,on the second place put double,on the third place string, and on the last place number of elements. I try something like this but it doesn’t work.

<?php
$array = array ('first' => 2.54, 'second' => "foo", 'third' => 1);
function myFunction($array)
{   $NewArray = array ();
    $[3] = 0;
foreach($array as $value)
{
    if(is_integer($value))
    {echo $NewArray[0] = $value.' ';}
    if(is_double($value))
    {echo $NewArray[1] = $value.' ';}
    if(is_string($value))
    {echo $NewArray[2] = $value.' ';}
    echo $NewArray[3] += 1 . ' ';}
    return $NewArray;}
MyFunction ($array);
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:47:06+00:00Added an answer on June 15, 2026 at 8:47 am

    Mathieu Imbert is right and you didn’t describe what was wrong when you ran the code. I corrected it based on what myFunction should return (as specified in your question).

    You shouldn’t concatenate those values with string ' ' unless you want it with the space after the value. Finally if you want number of elements on 3rd position in returned array, don’t concatenate the counter with ' ' – with that concatenation the counter would be '1 1 1 ' (for sample array from your question). Without it – 3.

    Here’s the corrected, tested code (I took the liberty of reformatting your code to make it easier to read and added print_r() for prettier output):

    <?php
        $array = array('first' => 2.54, 'second' => "foo", 'third' => 1);
        function myFunction($array) {
            $newArray = array();
            $newArray[3] = 0;
            foreach($array as $value) {
                if (is_integer($value)) {
                    $newArray[0] = $value;
                }
                if (is_double($value)) {
                    $newArray[1] = $value;
                }
                if (is_string($value)) {
                    $newArray[2] = $value;
                }
                $newArray[3] += 1;
            }
            return $newArray;
        }
        print_r(myFunction($array));
    ?>
    

    which outputs:

    Array
    (
        [3] => 3
        [1] => 2.54 
        [2] => foo 
        [0] => 1 
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I create a new array that contains all elements numbered nth to
Is there any way that I can create a new delegate type based on
I know that we can't overload operator with other meaning, we can't create new
How can I create a String object from a byte array byte arr[MAX_SIZE]; //
I am trying to create a method that removes duplicates from a 2d array.
in my application user can create an array of elements.E.g. he can assign a
I can create new Silverlight App with .NET RIA Services enabled. But when I
i created an additional content type called clients where I can create new client
How can create a New user in ORACLE with full access (alter, delete, select,
I can't create New Project on my Visual Web Developer 2008 Express with SP1.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.