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

  • SEARCH
  • Home
  • 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 8946043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:24:38+00:00 2026-06-15T12:24:38+00:00

Given an array of strings, such as $a: $a = array(zero, one, cat); I’m

  • 0

Given an array of strings, such as $a:

$a = array("zero", "one", "cat");

I’m looking to create array $b, populated “along the diagonal” with values from $a:

$b[0] = ["zero", "-", "-"]
$b[1] = ["-", "one", "-"]
$b[3] = ["-", "-", "cat"]

So far, I have:

function matrix($m, $n, $value) {
  return array_fill(0, $m, array_fill(0, $n, $value));
} // create a matrix (m,n) of $value

$a = array("zero", "one", "cat");  
$b = matrix(count($a),count($a),"'-'");  // create $b, filled with '-'

for($i = 0; $i < count($a); $i++){
  $b[$i][$i] = $a[$i];
} // fill matrix b with strings from a, along the diagonal

print_r($b);

In practice $a is going to be quite large, so I’m looking for a method with the lowest chance of bringing a server to its knees.

(Extra thanks given if you explain your version as if I were 8 years old.)

  • 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-15T12:24:39+00:00Added an answer on June 15, 2026 at 12:24 pm

    A method that “does not bring a server to its knees” in my opinion would avoid function calls and would also not use recursion, even though both may look cleaner in terms of code. Either way, this is a fairly simple problem.

    First, we create our dashes array:

    $dashes = array_fill(0, count($a), array_fill(0, count($a), '-'));
    

    You may notice a pattern based on dimensions:

    0,0    1,0    2,0
    0,1    1,1    2,1
    0,2    1,2    2,2
    

    Specifically, the X and Y coordinates match along the diagonal. That’s handy because we only have to know one or the other to know the slot to insert into our dashes array. The index of the item in $a qualifies as either (and both):

    foreach ($a as $num => $item) {
       $dashes[$num][$num] = $item;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I create an array in smarty from a given string like 22||33||50
Given the array below, how do I get values of object(stdClass) such as Pcv
This problem is taken from interviewstreet.com Given array of integers Y=y1,...,yn, we have n
Given an array with n values, for example: $arr[] = 'ABCDEFABC'; $arr[] = 'ABCDEFDEF';
Given two strings of equal length such that s1 = ACCT s2 = ATCT
Given 2 arrays of String values such as: String[] a1 = {A, B, C}
I need to convert a navigable map to a 2D String array. Below given
given an array: array = [16 16 16 22 23 23 23 25 52
Given an Array A of n subArrays Sn, how can I select the Array
Given an array of integers A[1...n-1] where N is the length of array A.

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.